zoukankan      html  css  js  c++  java
  • LeetCode 【困难】数据库-第615:平均工资:部门与公司比较(Case when ‘分类’ )

    题目:给如下两个表,写一个查询语句,求出在每一个工资发放日,每个部门的平均工资与公司的平均工资的比较结果 (高 / 低 / 相同)。

    1. 不同月份、不同部门的平均工资(group by 两个字段)

    select date_format(pay_date,'%Y-%m') as pay_month,department_id,avg(amount) as e_m_d_avg
    from salary s left join employee e
    on s.employee_id=e.employee_id
    group by department_id,pay_month
    

    2. 不同月份、公司的平均工资

    select date_format(pay_date,'%Y-%m') as pay_m,avg(amount) as company_avg
    from salary
    group by pay_m
    

    3.拼接、case when .条件. then .A. when .条件. then .B. else .C. end as 字段名

    select pay_month,department_id,
    case when e_m_d_avg > company_avg then 'higher' 
                                        when e_m_d_avg < company_avg then 'lower' else 'same' end as comparison
    from(
    select date_format(pay_date,'%Y-%m') as pay_month,department_id,avg(amount) as e_m_d_avg
    from salary s left join employee e
    on s.employee_id=e.employee_id
    group by department_id,pay_month
    ) a,
    
    (
    select date_format(pay_date,'%Y-%m') as pay_m,avg(amount) as company_avg
    from salary
    group by pay_m
    ) b
    where pay_month=pay_m 
    order by department_id,pay_month;
    

  • 相关阅读:
    集合的笼统介绍之Collection
    集合的笼统介绍之ArrayList
    final关键字+static关键字+匿名对象
    多态
    练习018:搜索插入位置
    练习017:实现strStr()
    练习016:移除元素
    练习015:删除排序数组中的重复项
    练习014:合并两个有序链表
    用JS实现链表
  • 原文地址:https://www.cnblogs.com/Tdazheng/p/14964571.html
Copyright © 2011-2022 走看看