SQL每日一题(20210903)
SQL每日一题(20220322)
SELECT b.pay_date,
b.department_id,
CASE
WHEN b.avgb >
a.avga THEN 'HIGHER'
WHEN b.avgb = a.avga THEN 'SAME'
ELSE 'LOWER' END comparison
FROM (SELECT date_format(ta.pay_date, '%Y-%m') pay_date, AVG(ta.amount) avga
FROM t0607a ta
GROUP BY date_format(ta.pay_date, '%Y-%m')) a,
(SELECT tb.department_id, date_format(ta.pay_date, '%Y-%m') pay_date, AVG(ta.amount) avgb
FROM t0607a ta,
t0607b tb
WHERE ta.employee_id = tb.employee_id
GROUP BY date_format(ta.pay_date, '%Y-%m'), tb.department_id) b
WHERE a.pay_date = b.pay_date
ORDER BY b.department_id, b.pay_date;
select PAY_MONTH,
DEPARTMENT_ID,
CASE
WHEN sum(money) >
sum(avg_money) THEN 'HIGHER'
WHEN sum(money) <
sum(avg_money) THEN 'LOWER'
ELSE 'SAME' END COMPARISON
from (select date_format(Pay_Date, '%Y-%m') PAY_MONTH,
Department_ID,amount,
avg(amount) over (partition by Department_ID,Pay_Date) as money,
avg(amount) over (partition by Pay_Date) avg_money
from T0607a a
inner join T0607b b on a.Employee_ID = b.Employee_ID) cc
group by DEPARTMENT_ID, PAY_MONTH
order by DEPARTMENT_ID