MySQL高阶2004-职员招聘人数

目录

题目

准备数据

分析数据

实现


题目

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

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

编写一个SQL查询,查找根据上述标准雇佣的高级员工和初级员工的数量。

任意顺序 返回结果表。

准备数据

sql 复制代码
Create table If Not Exists Candidates (employee_id int, experience ENUM('Senior', 'Junior'), salary int)
    Truncate table Candidates
    insert 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

sql 复制代码
select employee_id,sum(salary) over(order by salary) total1 from candidates
where experience = 'Senior';

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

sql 复制代码
with t1 as (
    select employee_id,sum(salary) over(order by salary) total1 from candidates
    where experience = 'Senior'
),t2 as (
    select max(total1) total from t1 where total1 <= 70000
)select * from t2;

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

sql 复制代码
with t1 as (
    select employee_id,sum(salary) over(order by salary) total1 from candidates
    where 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 candidates
    where experience = 'Junior'
)select * from t3;

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

sql 复制代码
with t1 as (
    select employee_id,sum(salary) over(order by salary) total1 from candidates
    where 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 candidates
    where 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);

实现

sql 复制代码
with t1 as (
    select employee_id,sum(salary) over(order by salary) total1 from candidates
    where 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 candidates
    where 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)
;
相关推荐
李少兄16 分钟前
使用 IntelliJ IDEA 连接到达梦数据库(DM)
java·数据库·intellij-idea
颜淡慕潇2 小时前
【数据库】Java 中 MongoDB 使用指南:步骤与方法介绍
java·数据库·sql·mongodb
叫我:松哥2 小时前
城市轨道交通网络客流大数据可视化分析系统----以某市交通网络客流数据为例
网络·数据库·python·信息可视化·前端框架·flask·echarts
big_noob2 小时前
centos7安装Redis单机版
数据库·redis·缓存·redis安装·redis安装教程·redis安装步骤·centos安装redis
小小不董3 小时前
Oracle表空间管理(三)
运维·数据库·oracle·ffmpeg·dba
木鬼与槐3 小时前
MySQL高阶1990-统计实验的数量
数据库·mysql
TPBoreas3 小时前
Mysql的锁你了解哪些?
数据库·mysql
Dontla3 小时前
mysql数据库锁以及用cp命令或rsync命令拷贝数据库数据时的注意事项(共享锁 Shared Locks、排他锁 Exclusive Locks)
数据库·mysql