Leetcode_数据库刷题_579. 查询员工的累计薪水

579题. 查询员工的累计薪水
SQL架构:

Create table If Not Exists Employee (Id int, Month int, Salary int)
Truncate table Employee
insert into Employee (Id, Month, Salary) values ('1', '1', '20')
insert into Employee (Id, Month, Salary) values ('2', '1', '20')
insert into Employee (Id, Month, Salary) values ('1', '2', '30')
insert into Employee (Id, Month, Salary) values ('2', '2', '30')
insert into Employee (Id, Month, Salary) values ('3', '2', '40')
insert into Employee (Id, Month, Salary) values ('1', '3', '40')
insert into Employee (Id, Month, Salary) values ('3', '3', '60')
insert into Employee (Id, Month, Salary) values ('1', '4', '60')
insert into Employee (Id, Month, Salary) values ('3', '4', '70')
insert into Employee (Id, Month, Salary) values ('1', '7', '90')
insert into Employee (Id, Month, Salary) values ('1', '8', '90')

Employee 表保存了一年内的薪水信息。
问题:请你编写 SQL 语句,对于每个员工,查询他除最近一个月(即最大月)之外,剩下每个月的近三个月的累计薪水(不足三个月也要计算)。
结果请按 Id 升序,然后按 Month 降序显示。
示例:
输入:

IdMonthSalary
1120
2120
1230
2230
3240
1340
3360
1460
3470

输出:

IdMonthSalary
1390
1250
1120
2120
33100
3240

解释:
员工 ‘1’ 除去最近一个月(月份 ‘4’),有三个月的薪水记录:月份 ‘3’ 薪水为 40,月份 ‘2’ 薪水为 30,月份 ‘1’ 薪水为 20。
所以近 3 个月的薪水累计分别为 (40 + 30 + 20) = 90,(30 + 20) = 50 和 20。

IdMonthSalary
1390
1250
1120

员工 ‘2’ 除去最近的一个月(月份 ‘2’)的话,只有月份 ‘1’ 这一个月的薪水记录。

IdMonthSalary
2120

员工 ‘3’ 除去最近一个月(月份 ‘4’)后有两个月,分别为:月份 ‘3’ 薪水为 60 和 月份 ‘2’ 薪水为 40。所以各月的累计情况如下:

IdMonthSalary
33100
3240

解答:

select Id, Month, Salary + IFNULL(L1, 0) + IFNULL(L2, 0) as Salary 
from (
    select *, 
    lag(Salary, 1) over(partition by Id order by Month) as L1, 
    lag(Salary, 2) over(partition by Id order by Month) as L2, 
    rank() over(partition by Id order by Month desc) as `RANK`
    from Employee
    ) as A
where `RANK` > 1
order by 1, 2 desc;

1、lag() over() 说明
lag(参数1,参数2,参数3) 参数1:表中列名 参数2:往下偏移多少位 参数3:超出行数时默认设置值
2、lead() over() 说明
lead(参数1,参数2,参数3) 参数1:表中列名 参数2:往上偏移多少位 参数3:超出行数时默认设置值

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-cumulative-salary-of-an-employee

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值