LeetCode(数据库)- 平均工资:部门与公司比较

简介: LeetCode(数据库)- 平均工资:部门与公司比较

题目链接:点击打开链接

题目大意:略。

解题思路:注意解决方案(2)中 date_format 用法。

AC 代码

-- 解决方案(1)
WITH t1 AS(
    SELECT s.*, e.department_id FROM salary s JOIN employee e ON s.employee_id = e.employee_id 
),
t2 AS(
    SELECT SUBSTRING(pay_date, 1, 7) pay_month, AVG(amount) co_avg FROM t1 GROUP BY pay_month
),
t3 AS(
    SELECT SUBSTRING(pay_date, 1, 7) pay_month, department_id, AVG(amount) de_avg FROM t1 GROUP BY pay_month, department_id
)
SELECT t2.pay_month, department_id, CASE
WHEN t2.co_avg = t3.de_avg THEN 'same'
WHEN t2.co_avg > t3.de_avg THEN 'lower'
ELSE 'higher'
END AS comparison
FROM t2 JOIN t3 ON t2.pay_month = t3.pay_month
-- 解决方案(2)
select department_salary.pay_month, department_id,
case
  when department_avg>company_avg then 'higher'
  when department_avg<company_avg then 'lower'
  else 'same'
end as comparison
from
(
  select department_id, avg(amount) as department_avg, date_format(pay_date, '%Y-%m') as pay_month
  from salary join employee on salary.employee_id = employee.employee_id
  group by department_id, pay_month
) as department_salary
join
(
  select avg(amount) as company_avg,  date_format(pay_date, '%Y-%m') as pay_month from salary group by date_format(pay_date, '%Y-%m')
) as company_salary
on department_salary.pay_month = company_salary.pay_month;
目录
相关文章
|
数据库
LeetCode(数据库)- 员工的直属部门
LeetCode(数据库)- 员工的直属部门
87 0
|
数据库
LeetCode(数据库)- 重新格式化部门表
LeetCode(数据库)- 重新格式化部门表
109 0
|
数据库
LeetCode(数据库)- 部门工资前三高的所有员工
LeetCode(数据库)- 部门工资前三高的所有员工
81 0
LeetCode(数据库)- 部门工资前三高的所有员工
|
数据库
LeetCode(数据库)- 部门工资最高的员工
LeetCode(数据库)- 部门工资最高的员工
93 0
|
SQL Oracle 关系型数据库
【LeetCode-SQL高手挑战】—185. 部门工资前三高的所有员工
【LeetCode-SQL高手挑战】—185. 部门工资前三高的所有员工
294 0
【LeetCode-SQL高手挑战】—185. 部门工资前三高的所有员工
|
SQL 算法
​LeetCode刷题实战184:部门工资最高的员工
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
120 0
​LeetCode刷题实战184:部门工资最高的员工
|
3天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
3天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
12 0
|
3天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法