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

边栏推荐
- 利用opencv读取图片,重命名。
- HCIP BGP建邻实验
- 抖音关键词搜索商品-API工具
- SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
- 使用Keras构建GAN,以Mnist为例
- HCIP BGP built adjacent experiment
- MySQL之函数
- pytorch调整模型学习率
- My approval of OA project (inquiry & meeting signature)
- My meeting of the OA project (meeting seating & review)
猜你喜欢

HCIP OSPF dynamic routing protocol

TOP2 Add two numbers

HCIA experiment

OA Project Pending Meeting & History Meeting & All Meetings

空间金字塔池化 -Spatial Pyramid Pooling(含源码)

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

HCIP BGP建邻、联邦、汇总实验

1688商品详情接口

SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)

拼多多API接口(附上我的可用API)
随机推荐
iptables的状态
HCIP OSPF/MGRE综合实验
淘宝API接口参考
《Generative Adversarial Networks》
《Show and Tell: A Neural Image Caption Generator》论文解读
会议OA项目之我的会议
windows10安全中心显示“修正未完成”
ETCD containerized to build a cluster
My meeting of the OA project (meeting seating & review)
cloudreve使用体验
Top20 bracket matching
Especially the redis
HCIP MPLS/BGP Comprehensive Experiment
Class definition, class inheritance, and the use of super
损失函数——负对数似然
【LeetCode】851.喧闹与富有(思路+题解)
lvm multi-disk mount, combined use
Concurrent programming in eight-part essay
防火墙-0-管理地址
pytorch下tensorboard可视化深坑