当前位置: 首页 > news >正文

MySQL高阶2004-职员招聘人数

目录

题目

准备数据

分析数据

实现


题目

一家公司想雇佣新员工。公司的工资预算是 70000 美元。公司的招聘标准是:

  1. 雇佣最多的高级员工。
  2. 在雇佣最多的高级员工后,使用剩余预算雇佣最多的初级员工。

编写一个SQL查询,查找根据上述标准雇佣的高级员工和初级员工的数量。
按 任意顺序 返回结果表。

准备数据

Create table If Not Exists Candidates (employee_id int, experience ENUM('Senior', 'Junior'), salary int)Truncate table Candidatesinsert into Candidates (employee_id, experience, salary) values ('1', 'Junior', '10000')insert into Candidates (employee_id, experience, salary) values ('9', 'Junior', '10000')insert into Candidates (employee_id, experience, salary) values ('2', 'Senior', '20000')insert into Candidates (employee_id, experience, salary) values ('11', 'Senior', '20000')insert into Candidates (employee_id, experience, salary) values ('13', 'Senior', '50000')insert into Candidates (employee_id, experience, salary) values ('4', 'Junior', '40000');

分析数据

第一步:筛选"Senior"的累计salary总和

因为根据salary排序,所以开窗函数sum中salary相同的值相同,所以employee_id = 2 或 11的薪水都是40000

select employee_id,sum(salary) over(order by salary) total1 from candidates
where experience = 'Senior';

第二步:t2子查询选择最大值小于70000

with t1 as (select employee_id,sum(salary) over(order by salary) total1 from candidateswhere experience = 'Senior'
),t2 as (select max(total1) total from t1 where total1 <= 70000
)select * from t2;

第三步:筛选"Junior"的累计salary总和

with t1 as (select employee_id,sum(salary) over(order by salary) total1 from candidateswhere experience = 'Senior'
),t2 as (select max(total1) total from t1 where total1 <= 70000
),t3 as (select employee_id,sum(salary) over(order by salary) total2 from candidateswhere experience = 'Junior'
)select * from t3;

第四步:第一个子查询从t1中选择"Senior"中小于或等于70000,第二个select子查询再从t2和t3中选择"Junior"出小于70000减去第一个select子查询.最后通过union all将两个结果结合起来.

with t1 as (select employee_id,sum(salary) over(order by salary) total1 from candidateswhere experience = 'Senior'
),t2 as (select max(total1) total from t1 where total1 <= 70000
),t3 as (select employee_id,sum(salary) over(order by salary) total2 from candidateswhere experience = 'Junior'
)
select 'Senior' as experience,count(distinct  employee_id) accepted_candidates from t1
where total1 <= 70000
union all
select 'Junior' as experience,count(distinct  employee_id) accepted_candidates from t2,t3
where total2 < 70000 - ifnull(total,0);

实现

with t1 as (select employee_id,sum(salary) over(order by salary) total1 from candidateswhere experience = 'Senior'
),t2 as (select max(total1) total from t1 where total1 <= 70000
),t3 as (select employee_id,sum(salary) over(order by salary) total2 from candidateswhere experience = 'Junior'
)
select 'Senior' as experience,count(distinct  employee_id) accepted_candidates from t1
where total1 <= 70000
union all
select 'Junior' as experience,count(distinct  employee_id) accepted_candidates from t2,t3
where total2 < 70000 - ifnull(total,0)
;

相关文章:

  • 713. 乘积小于 K 的子数组 滑动窗口
  • Python Pandas数据处理效率提升指南
  • 【笔记】自动驾驶预测与决策规划_Part4_时空联合规划
  • 【GUI设计】基于Matlab的图像去噪GUI系统(8),matlab实现
  • 构建企业数字化转型的战略基石——TOGAF框架的深度解析
  • 物理学基础精解【26】
  • 录屏+GIF一键生成,2024年费软件大揭秘
  • Kubernetes 中 Pod 和 Node 的关系详解
  • 代码整洁之道 — 1 命名规范
  • C++——有3个学生,每个学生的数据包括:学号、姓名、3门课的成绩,从键盘输入三个学生的数据。要求打印学生三门课的平均分。
  • SpringBoot使用EasyPoi根据模板导出word or pdf
  • 什么是网络准入控制系统?2024年有哪些好用的网络准入控制系统?
  • 2024/10/1 操作系统大题专训之文件
  • SpringBoot实现社区医院数据集成解决方案
  • AWS Network Firewall -NAT网关配置只应许白名单域名出入站
  • [ JavaScript ] 数据结构与算法 —— 链表
  • 《Java编程思想》读书笔记-对象导论
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • Java IO学习笔记一
  • magento2项目上线注意事项
  • node学习系列之简单文件上传
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • vue-loader 源码解析系列之 selector
  • 分布式事物理论与实践
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 如何解决微信端直接跳WAP端
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 我们雇佣了一只大猴子...
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • ​香农与信息论三大定律
  • (35)远程识别(又称无人机识别)(二)
  • (7) cmake 编译C++程序(二)
  • (70min)字节暑假实习二面(已挂)
  • (bean配置类的注解开发)学习Spring的第十三天
  • (day 12)JavaScript学习笔记(数组3)
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (solr系列:一)使用tomcat部署solr服务
  • (笔试题)分解质因式
  • (分布式缓存)Redis哨兵
  • (附源码)php投票系统 毕业设计 121500
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (四)stm32之通信协议
  • (一)UDP基本编程步骤
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • ***原理与防范
  • .NET CORE Aws S3 使用
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • ::before和::after 常见的用法
  • :中兴通讯为何成功
  • [ C++ ] STL---stack与queue
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [ 物联网 ]拟合模型解决传感器数据获取中数据与实际值的误差的补偿方法
  • [Android]使用Retrofit进行网络请求
  • [BUUCTF]-Reverse:reverse3解析