You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
select
distinct date_format(pay_date, '%Y-%m') as pay_month,
department_id,
(case when avg_department > avg_company then 'higher'
when avg_department < avg_company then 'lower'
else 'same'
end) as comparison
from (
select
pay_date,
department_id,
avg(amount) as avg_department
from salary join employee using(employee_id)
group by pay_date, department_id
) as temp1 join (
select pay_date, avg(amount) as avg_company from salary group by pay_date
) as temp2 using(pay_date)
--- 等价于select
pay_month,
department_id,
(case when avg_department > avg_company then 'higher'
when avg_department < avg_company then 'lower'
else 'same'
end) as comparison
from (
select
date_format(pay_date, '%Y-%m') as pay_month,
department_id,
avg(amount) as avg_department
from salary join employee using(employee_id)
group by pay_month, department_id
) as temp1 join (
select
date_format(pay_date, '%Y-%m') as pay_month,
avg(amount) as avg_company
from salary group by pay_month
) as temp2 using(pay_month)
题目
求出每一薪资发放日,每个部门的平均薪资与公司的平均薪资比较的结果(高/低/相同)
SQL:方法一
解析
这题有两个重点:
计算部门每个月的平均薪资,将
salary
和employee
用employee_id
连接,并且按照 ,计算出部门薪资平均值avg_department
,pay_month
和department_id
进行分组,将它作为临时表temp1
计算公司每个月的平均薪资比较简单,直接对
salary
表按照pay_date
进行分组,并且计算出公司薪资平均值avg_company
,将它作为临时表temp2
将
temp1
和temp2
用pay_date
连接起来,使用case ... when ... end
语句比较avg_department
和avg_company
的大小后输出same
、higher
、lower
因为这里输出的都是日期
date
,所以这里要使用date_format()
对它进行日期格式化。这里要注意一点的是因为
temp1
和temp2
都是用date
分组的,而最后查出来的数据只需要月份,所以这里可能会出现重复的数据,需要在最后使用distinct
去重,或者呢在temp1
和temp2
是就直接使用month
进行分组。The text was updated successfully, but these errors were encountered: