算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 平均工资:部门与公司比较,我们先来看题面:
https://leetcode.cn/problems/average-salary-departments-vs-company/

解题
先求出每个部门每个月的平均工资
再求公司每个月的平均工资
连表查,case when 比较
select t1.pay_month, t1.department_id,
case when t1.am > t2.am then 'higher' when t1.am < t2.am then 'lower' else 'same' end comparison
from
(
select DATE_FORMAT(pay_date,'%Y-%m') pay_month, department_id , avg(amount) am
from salary s, employee e
where s.employee_id = e.employee_id
GROUP BY pay_month, department_id
) t1,
(
select DATE_FORMAT(pay_date,'%Y-%m') pay_month, avg(amount) am from salary GROUP BY pay_month
) t2
where t1.pay_month = t2.pay_month上期推文:
LeetCode1-600题汇总,希望对你有点帮助!
LeetCode刷题实战601:体育馆的人流量
LeetCode刷题实战602:好友申请 II :谁有最多的好友
LeetCode刷题实战603:连续空余座位
LeetCode刷题实战604:迭代压缩字符串
LeetCode刷题实战605:种花问题
LeetCode刷题实战606:根据二叉树创建字符串
LeetCode刷题实战607:销售员
LeetCode刷题实战608:树节点
LeetCode刷题实战609:在系统中查找重复文件
LeetCode刷题实战610:判断三角形
LeetCode刷题实战611:有效三角形的个数
LeetCode刷题实战612:平面上的最近距离
LeetCode刷题实战613:直线上的最近距离
LeetCode刷题实战614:二级关注者