当前位置:网站首页>每日sql--统计员工近三个月的总薪水(不包括最新一个月)
每日sql--统计员工近三个月的总薪水(不包括最新一个月)
2022-08-11 05:36:00 【吃再多糖也不长胖】
每日sql–统计员工近三个月的总薪水(不包括最新一个月)
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;

边栏推荐
猜你喜欢

抖音获取douyin分享口令url API 返回值说明

一个小时快速熟悉MySQL基本用法

FusionCompute8.0.0实验(0)CNA及VRM安装(2280v2)

radix-4 FFT 原理和C语言代码实现

lvm multi-disk mount, combined use

TOP2 Add two numbers

淘宝sku API 接口(PHP示例)

deepin v20.6+cuda+cudnn+anaconda(miniconda)

The ramdisk practice 1: the root file system integrated into the kernel

ETCD containerized to build a cluster
随机推荐
抖音关键词搜索商品-API工具
Memory debugging tools Electric Fence
抖音API接口
lvm multi-disk mount, combined use
radix-4 FFT 原理和C语言代码实现
windows10安全中心显示“修正未完成”
My meeting of the OA project (meeting seating & review)
iptables入门
HCIP 重发布/路由策略实验
Top20 bracket matching
会议OA项目之我的会议
detectron2,手把手教你训练mask_rcnn
Numpy_备注
torch.cat()使用方法
损失函数——交叉熵
Local yum source build
华为防火墙-4-安全策略
拼多多API接口(附上我的可用API)
MySQl进阶之索引结构
HCIP--交换基础