615 Average Salary: Departments VS Company

https://leetcode.com/articles/average-salary-departments-vs-company/

select date_formate(pay_date,'%Y-%m') pay_month, e.department_id,

  (case when avg(amount) = com_avg  then 'same'

  when avg(amount) > com_avg  then 'higher'

  else 'lower' end) comparison  

from salary s1

left join employee e on s.employee_id  = e.employee_id 

inner join

(select date_format(pay_date, '%Y-%m'pay_month, avg(amount) com_avg from salary group by date_formate(pay_date,'%Y-%m')) s2

on s2.pay_month= date_formate(s1.pay_date,'%Y-%m')

group by e.department_id,date_formate(pay_date,'%Y-%m')

 

posted @ 2018-10-23 11:22  ffeng0312  阅读(254)  评论(0)    收藏  举报