LeetCode MySQL 579. 查询员工的累计薪水(over(rows n preceding)选取窗口1+前n条)

文章目录

1. 题目

Employee 表保存了一年内的薪水信息。

请你编写 SQL 语句,对于每个员工,查询他除最近一个月(即最大月)之外,剩下每个月的近三个月的累计薪水(不足三个月也要计算)。

结果请按 Id 升序,然后按 Month 降序显示。

示例:
输入:
| Id | Month | Salary |
|----|-------|--------|
| 1  | 1     | 20     |
| 2  | 1     | 20     |
| 1  | 2     | 30     |
| 2  | 2     | 30     |
| 3  | 2     | 40     |
| 1  | 3     | 40     |
| 3  | 3     | 60     |
| 1  | 4     | 60     |
| 3  | 4     | 70     |

输出:
| Id | Month | Salary |
|----|-------|--------|
| 1  | 3     | 90     |
| 1  | 2     | 50     |
| 1  | 1     | 20     |
| 2  | 1     | 20     |
| 3  | 3     | 100    |
| 3  | 2     | 40     |
 

解释:
员工 '1' 除去最近一个月(月份 '4'),
有三个月的薪水记录:
月份 '3' 薪水为 40,
月份 '2' 薪水为 30,
月份 '1' 薪水为 20。

所以近 3 个月的薪水累计分别为 
(40 + 30 + 20) = 90(30 + 20) = 5020| Id | Month | Salary |
|----|-------|--------|
| 1  | 3     | 90     |
| 1  | 2     | 50     |
| 1  | 1     | 20     |

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

| Id | Month | Salary |
|----|-------|--------|
| 2  | 1     | 20     |

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

| Id | Month | Salary |
|----|-------|--------|
| 3  | 3     | 100    |
| 3  | 2     | 40     |

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-cumulative-salary-of-an-employee
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

# Write your MySQL query statement below
select Id, Month, 
        sum(Salary) over(partition by Id order by Month rows 2 preceding) Salary
from Employee
where (Id, Month) not in
(
    select Id, max(Month)
    from Employee
    group by Id
)
order by Id, Month desc

我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

猜你喜欢

转载自blog.csdn.net/qq_21201267/article/details/107729626