我真的很困惑如何为以下语句编写查询。
如何显示销售额(总数)最高的前 5 名员工,并将职位显示为字段。请注意,如果两名员工的总销售额相同,他们应该获得相同的职位,换句话说,前 5 名员工可能会返回超过 5 名员工?
任何人都可以为此提出答案吗?
我真的很困惑如何为以下语句编写查询。
如何显示销售额(总数)最高的前 5 名员工,并将职位显示为字段。请注意,如果两名员工的总销售额相同,他们应该获得相同的职位,换句话说,前 5 名员工可能会返回超过 5 名员工?
任何人都可以为此提出答案吗?
Something like:
WITH cte AS (
SELECT
Rank() OVER (ORDER BY SUM(salary) DESC ) AS [Rank]
, SUM(salary) as sum
, Employee
FROM
[table]
GROUP BY
Employee
)
SELECT *
FROM cte
WHERE [RANK] < 5
ORDER BY sum DESC
I re-read your question and it seems that you're looking for the top count of sales rather than sum of salary, but of course, in that case you can just use COUNT(sales)
in the OVER statement.