1、题目描述
来源:力扣(LeetCode)
2、解题思路
1# 首先增加子表,查询每月公司的平均工资
select date_format(pay_date,'%Y-%m') as pay_m,avg(amount) as com_t
from salary
group by pay_m
2# 然后增加子表,按部门和月度分类,查询平均工资
select date_format(pay_date,'%Y-%m') as pay_month,department_id,avg(amount) as com
from salary s left join employee e
on s.employee_id=e.employee_id
group by department_id,pay_month
3# 然后2表连接,进行平均值对比,和分类
case when com>com_t then 'higher' when com<com_t then 'lower' else 'same' end as comparison
3、提交记录
select pay_month,department_id,case when com>com_t then 'higher'
when com<com_t then 'lower' else 'same' end as comparison
from(
select date_format(pay_date,'%Y-%m') as pay_month,department_id,avg(amount) as com
from salary s left join employee e
on s.employee_id=e.employee_id
group by department_id,pay_month
)m1,
(select date_format(pay_date,'%Y-%m') as pay_m,avg(amount) as com_t
from salary
group by pay_m)m2
where pay_month=pay_m
order by department_id,pay_month