当前位置:网站首页>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;
边栏推荐
- Record a Makefile just written
- 八股文之jvm
- Map Reduce
- 训练分类器
- 一种用于EEG超扫描研究的分析流程
- 图文带你理解什么是Few-shot Learning
- mmdetection的安装和训练、测试didi数据集的步骤(含结果)
- HCIP BGP neighbor building, federation, and aggregation experiments
- HCIP Republish/Routing Policy Experiment
- Find the shops that have sold more than 1,000 yuan per day for more than 30 consecutive days in the past six months
猜你喜欢
随机推荐
华为防火墙-3-应用过滤
八股文之redis
损失函数——负对数似然
查看内核版本和发行版版本
核方法 Kernel method
抖音API接口大全
MySQL之函数
Open Set Domain Adaptation 开集领域适应
皮质-皮质网络的多尺度交流
Class definition, class inheritance, and the use of super
Find the shops that have sold more than 1,000 yuan per day for more than 30 consecutive days in the past six months
HCIP OSPF dynamic routing protocol
利用opencv读取图片,重命名。
daily sql - user retention rate for two days
每日sql - 判断+聚合
淘宝sku API 接口(PHP示例)
iptables 流量统计
OA project meeting notice (query & whether attending & feedback for details)
Redis + lua implements distributed interface current limiting implementation scheme
京东商品详情API调用实例讲解