这是迄今为止的情景。我应该在公司中显示第四名(仅限第四名,而不是前四名)有偿人员。但是我不想使用子查询。那就是问题所在。我似乎无法找到一种只显示第四种方式的方法。
这是我目前的代码
SELECT TOP 5 E1.BusinessEntityID, SUM(E2.PayFrequency*E2.Rate) AS Pay
FROM HumanResources.EmployeePayHistory AS E1
INNER JOIN HumanResources.EmployeePayHistory AS E2
ON E1.BusinessEntityID=E2.BusinessEntityID
GROUP BY E1.BusinessEntityID
ORDER BY SUM(E2.PayFrequency*E2.Rate) DESC
提前谢谢
答案 0 :(得分:4)
OFFSET/FETCH可以轻松完成此操作:
SELECT TOP 5 E1.BusinessEntityID, SUM(E2.PayFrequency*E2.Rate) AS Pay
FROM HumanResources.EmployeePayHistory AS E1
INNER JOIN HumanResources.EmployeePayHistory AS E2
ON E1.BusinessEntityID=E2.BusinessEntityID
GROUP BY E1.BusinessEntityID
ORDER BY SUM(E2.PayFrequency*E2.Rate) DESC
OFFSET 3 ROWS FETCH NEXT 1 ROWS ONLY;
答案 1 :(得分:1)
您也可以使用行号
执行此操作with cte
as
(
SELECT TOP 5 E1.BusinessEntityID, SUM(E2.PayFrequency*E2.Rate) AS Pay,row_number() over (ORDER BY SUM(E2.PayFrequency*E2.Rate) DESC) as rn
FROM HumanResources.EmployeePayHistory AS E1
INNER JOIN HumanResources.EmployeePayHistory AS E2
ON E1.BusinessEntityID=E2.BusinessEntityID
GROUP BY E1.BusinessEntityID
ORDER BY SUM(E2.PayFrequency*E2.Rate) DESC
)
select * from cte where rn=4