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;
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)
;