zoukankan      html  css  js  c++  java
  • 184. 部门工资最高的员工

    184. 部门工资最高的员工
    
    select 
        d.Name as Department,
        e.Name as Employee,
        e.Salary 
    from 
        Employee e,Department d 
    where
        e.DepartmentId=d.id 
        and
        (e.Salary,e.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId);

    /* Write your T-SQL query statement below */
    
    -- 每个部门最高
    SELECT S.NAME, S.EMPLOYEE, S.SALARY
      FROM (SELECT D.NAME,
                   T.NAME EMPLOYEE,
                   T.SALARY,
                   ROW_NUMBER() OVER(PARTITION BY T.DEPARTMENTID ORDER BY T.SALARY DESC) RN
              FROM EMPLOYEE T
              LEFT JOIN DEPARTMENT D
                ON T.DEPARTMENTID = D.ID) S
     WHERE S.RN = 1
    -- 每个部门前2高
    SELECT S.NAME, S.EMPLOYEE, S.SALARY
      FROM (SELECT D.NAME,
                   T.NAME EMPLOYEE,
                   T.SALARY,
                   ROW_NUMBER() OVER(PARTITION BY T.DEPARTMENTID ORDER BY T.SALARY DESC) RN
              FROM EMPLOYEE T
              LEFT JOIN DEPARTMENT D
                ON T.DEPARTMENTID = D.ID) S
     WHERE S.RN <= 2
    -- 每个部门第一第三高
    SELECT S.NAME, S.EMPLOYEE, S.SALARY
      FROM (SELECT D.NAME,
                   T.NAME EMPLOYEE,
                   T.SALARY,
                   ROW_NUMBER() OVER(PARTITION BY T.DEPARTMENTID ORDER BY T.SALARY DESC) RN
              FROM EMPLOYEE T
              LEFT JOIN DEPARTMENT D
                ON T.DEPARTMENTID = D.ID) S
     WHERE S.RN = 1 OR S.RN = 3
  • 相关阅读:
    软件测试作业4
    Android基础知识每日记(持续更新)
    二叉树操作总结
    No.223 Rectangle Area
    No.66 Plus One
    No.80 Remove Duplicates from Sorted Array ||
    No.27 Remove Element
    No.26 Remove Duplicates from Sorted Array
    No.9 Palindrome Number
    No.219 Contains Duplicate ||
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/14177725.html
Copyright © 2011-2022 走看看