当前位置: 首页> 游戏> 攻略 > 宁波免费建站外包公司_扬中人_百度网盘资源共享_万网域名管理入口

宁波免费建站外包公司_扬中人_百度网盘资源共享_万网域名管理入口

时间:2025/7/12 1:30:49来源:https://blog.csdn.net/weixin_58305115/article/details/142662419 浏览次数:0次
宁波免费建站外包公司_扬中人_百度网盘资源共享_万网域名管理入口

目录

题目

准备数据

分析数据

实现


题目

一家公司想雇佣新员工。公司的工资预算是 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)
;

关键字:宁波免费建站外包公司_扬中人_百度网盘资源共享_万网域名管理入口

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: