Ai
1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-0579.test 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
逸扬 提交于 2022-05-01 21:45 +08:00 . 512-534-574-577-578-579-585-596-597-601 (10)
# $579. 查询员工的累计薪水
# https://leetcode-cn.com/problems/find-cumulative-salary-of-an-employee/
# SQL架构
Create table If Not Exists Employee (id int, month int, salary int);
Truncate table Employee;
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');
insert into Employee (id, month, salary) values ('1', '7', '90');
insert into Employee (id, month, salary) values ('1', '8', '90');
# Write your MySQL query statement below
select
e1.id,
e1.month,
ifnull(e1.salary, 0) + ifnull(e2.salary, 0) + ifnull(e3.salary, 0) as salary
from
(
select
id,
max(month) as month
from
Employee
group by
id
having
count(*) > 1
) maxmonth
left join Employee e1 on (
maxmonth.id = e1.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
id asc,
month desc;
# clean-up
drop table Employee;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助