题解 | #统计出当前各个title类型对应的员工当前薪水对应的平均工资#

统计出当前各个title类型对应的员工当前薪水对应的平均工资

http://www.nowcoder.com/practice/c8652e9e5a354b879e2a244200f1eaae

  • 开窗函数
    Mysql 8.0以上才可以。
    select distinct t.title, avg(s.salary) over (partition by t.title) as `avg(s.salary)`
    from titles as t inner join salaries s
      on t.emp_no = s.emp_no
    order by `avg(s.salary)`;
  • 组合查询
    查询表包含emp_no、title和salary(当前值)
    select s.emp_no, t.title, s.salary
    from titles as t
           left join salaries as s
                     on t.emp_no = s.emp_no
    where t.to_date = '9999-01-01'
    and s.to_date = '9999-01-01';
    计算字段
    select title, avg(salary)
    from (select t.title, s.salary
        from titles as t
                 left join salaries as s
                           on t.emp_no = s.emp_no
        where t.to_date = '9999-01-01'
          and s.to_date = '9999-01-01') as a1
    group by title;
全部评论

相关推荐

monococonut:我体感,wxg 双非同学还蛮多的,是真的英雄不问出处
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 13:41
已编辑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务