当前位置:网站首页>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;
边栏推荐
猜你喜欢
Especially the redis
MySQL01
亚马逊获得AMAZON商品详情 API 返回值说明
HCIP BGP built adjacent experiment
亚马逊API接口大全
HCIP-Spanning Tree (802.1D, Standard Spanning Tree/802.1W: RSTP Rapid Spanning Tree/802.1S: MST Multiple Spanning Tree)
每日sql -查询至少有5名下属的经理和选举
HCIP WPN experiment
拼多多API接口(附上我的可用API)
unable to extend table xxx by 1024 in tablespace xxxx
随机推荐
mmdetection的安装和训练、测试didi数据集的步骤(含结果)
皮质-皮质网络的多尺度交流
sql--7天内(含当天)购买次数超过3次(含),且近7天的购买金额超过1000的用户
HCIP OSPF动态路由协议
每日sql:求好友申请通过率
一个小时快速熟悉MySQL基本用法
亚马逊获得AMAZON商品详情 API 返回值说明
强烈推荐一款好用的API接口
《Generative Adversarial Networks》
MySQL之函数
华为防火墙会话 session table
图的拉普拉斯矩阵
从mask-rcnn到shp
Pinduoduo API interface (attach my available API)
Cobbleland 博览会 基础系列 1
亚马逊API接口大全
HCIP BGP建邻、联邦、汇总实验
numpy和tensor增加或删除一个维度
OA Project Pending Meeting & History Meeting & All Meetings
torch.cat()用法