如何在组语句中获得单行值?

时间:2012-05-30 02:14:42

标签: sql database oracle11g

我有两个表JobTableEmployeeTable,其中包含以下数据:

EmployeeTable中:

EmpId    Salary
1          10
2          20
3          30
4          40
5          50
6          60

JobTable:

JobId    EmpId
A          1
A          2
B          3 
B          4
C          5
C          6

我需要一个SQL语句,它将以每个Job的最低工资返回Employee的EmpId

2 个答案:

答案 0 :(得分:1)

您可以像这样使用RANK()函数:

WITH ranked AS (
  SELECT
    j.JobId,
    e.EmpId,
    e.Salary,
    RANK() OVER (PARTITION BY j.JobId ORDER BY e.Salary) AS rnk
  FROM JobTable j
  INNER JOIN EmployeeTable e ON j.EmpId = e.EmpId
)
SELECT
  JobId,
  EmpId,
  Salary,
FROM ranked
WHERE rnk = 1

答案 1 :(得分:0)

嗯,给这个人一个机会:

SELECT st.EmpID, min(st.salary) 
FROM SalaryTable st INNER JOIN JobTable jt ON st.EmpID=jt.EmpID 
WHERE jt.JobID = 'A' 
GROUP BY st.EmpID