查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
查找薪水变动超过15次的员工号emp_no以及其对应的变动次数t
https://www.nowcoder.com/practice/6d4a4cff1d58495182f536c548fee1ae?tpId=82&tqId=29758&rp=1&ru=%2Fta%2Fsql&qru=%2Fta%2Fsql%2Fquestion-ranking
题目:查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
题目隐含:
1.一条记录就算一次涨幅(严格意义上,从第二条记录算起才算一次涨幅)
2.任何一条记录都算一次涨幅(严格意义上,必须比上一条记录的工资高才算一次涨幅)
分组+聚合函数
select emp_no, count() t
from salaries
group by emp_no
having t > 15;
补充:如果考虑第二条记录算起才算一次涨幅,应该写:
select amp_no, count(distinct from_date)-1 as t
from salaries
group by emp_no
having t>15
补充:如果考虑严格意义上的涨幅,应该写:
select a.emp_no, count() t
from salaries a inner join salaries b
on a.emp_no=b.emp_no and a.to_date=b.from_date
where a.salary < b.salary
group by a.emp_no
having t>15
收起