当前位置:网站首页>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;

边栏推荐
- 拼多多API接口大全
- HCIP 重发布/路由策略实验
- HCIP-Spanning Tree (802.1D, Standard Spanning Tree/802.1W: RSTP Rapid Spanning Tree/802.1S: MST Multiple Spanning Tree)
- Pinduoduo api interface application example
- Monte Carlo
- HCIP MPLS/BGP Comprehensive Experiment
- 《Show, Attend and Tell: Neural Image Caption Generation with Visual Attention》论文阅读(详细)
- Top20 bracket matching
- HCIA knowledge review
- Pinduoduo API interface (attach my available API)
猜你喜欢
随机推荐
使用Keras构建GAN,以Mnist为例
每日sql-统计各个专业人数(包括专业人数为0的)
bash的命令退出状态码
Redis + lua implements distributed interface current limiting implementation scheme
《猪猪1984》NFT 作品集将上线 The Sandbox 市场平台
HCIP BGP built adjacent experiment
Monte Carlo
阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解
每日sql -用户两天留存率
Pinduoduo api interface application example
iptables nat
Especially the redis
华为防火墙-7-dhcp
命令输出给变量
WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
HCIP BGP建邻、联邦、汇总实验
HCIP OSPF动态路由协议
pytorch下tensorboard可视化深坑
HCIP实验(pap、chap、HDLC、MGRE、RIP)
SATA、SAS、SSD三种硬盘存储性能数据









