当前位置:网站首页>Daily sql--statistics the total salary of employees in the past three months (excluding the latest month)
Daily sql--statistics the total salary of employees in the past three months (excluding the latest month)
2022-08-11 07:14:00 【Eating too much sugar will not gain weight】
每日sql–Calculate the total salary of employees in the past three months(Excluding the latest month)
DDL
Create table If Not Exists Employee (Id int, Month int, Salary int);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);
sql
select e1.Id,e1.month, case when e1.salary is null then 0 else e1.salary end +case when e2.salary is null then 0 else e2.salary end+case when e3.salary is null then 0 else e3.salary end AS Salary
from
(select Id,max(month)as month from Employee group by Id having count(1)>1) maxmonth
left join Employee e1 on e1.Id = maxmonth.Id and maxmonth.month >e1.month
left join Employee e2 on e2.Id = e1.Id and e2.month = e1.month-1
left join Employee e3 on e3.Id = e1.Id and e3.month = e1.month-2
ORDER BY e1.id ASC , e1.month DESC;
边栏推荐
猜你喜欢
随机推荐
华为防火墙-6
numpy和tensor增加或删除一个维度
HCIP WPN experiment
每日sql - 判断+聚合
Class definition, class inheritance, and the use of super
HCIP OSPF/MGRE综合实验
OA项目之待开会议&历史会议&所有会议
MySQL导入导出&视图&索引&执行计划
Xshell如何连接虚拟机
ETCD Single-Node Fault Emergency Recovery
配置dns服务
快速了解集成学习
MySQL01
Eight-legged text jvm
每日sql-求2016年成功的投资总和
HCIP BGP neighbor building, federation, and aggregation experiments
Numpy_备注
八股文之jvm
图文带你理解什么是Few-shot Learning
核方法 Kernel method