有趣的SQL-搜索前三名问题

今天看到这样个问题,比如找出每个部门里薪资在前三的所有员工信息

具体数据和SQL是这样的

WITH Employee AS(
       SELECT 1 Id, 'joe' Name,70000 SALARY,1 DepartmentId FROM DUAL UNION ALL
       SELECT 2 Id, 'Henry' Name,80000 SALARY,2 DepartmentId FROM DUAL UNION ALL
       SELECT 3 Id, 'Sam' Name,60000 SALARY,2 DepartmentId FROM DUAL UNION ALL
       SELECT 4 Id, 'Max' Name,90000 SALARY,1 DepartmentId FROM DUAL UNION ALL
       SELECT 5 Id, 'Janet' Name,69000 SALARY,1 DepartmentId FROM DUAL UNION ALL
       SELECT 6 Id, 'Randy' Name,85000 SALARY,1 DepartmentId FROM DUAL
      ),
      Department AS (
       SELECT 1 Id, 'IT' Name FROM DUAL UNION ALL
       SELECT 2 Id, 'Sales' Name FROM DUAL
      )
     
      SELECT T1.Name,T2.*
      FROM
           Department T1
          ,Employee   T2
      WHERE T1.Id= T2.DepartmentId
      AND 3>
      (
        SELECT COUNT(distinct T3.SALARY) RN
        FROM  Employee T3
        WHERE T2.DepartmentId = T3.DepartmentId
        AND T2.SALARY < T3.SALARY
      )
      ORDER BY T2.DEPARTMENTID,T2.SALARY

上一篇:【Oracle】并行等待之PX Deq Credit: send blkd


下一篇:C# 数据操作系列 - 4. 自己实现一个ORM