[英]SQL Select record of the max aggregate
I need to select the "job role desc" of the maximum of the aggregates I have achieved. 我需要选择我已实现的最大汇总数的“职务”。 The table looks like follows. 该表如下所示。
[Job Role Description] | [Number Of Placements]
------------------------------------------------
Training BI 24
System Analyst 23
Data Analyst 24
Data consultant 25
DB programmer 24
The job_role_desc
column is from another table joined by job_role_id
. job_role_desc
列来自另一个由job_role_id
连接的job_role_id
。 I used the following code to achieve this. 我使用以下代码实现了这一目标。
SELECT
job_role_dim.job_role_desc AS "Job Role Description" ,
SUM(fact_accounts.no_of_placements) AS "Number Of Placements"
FROM
fact_accounts
INNER JOIN
job_role_dim ON job_role_dim.job_role_id = fact_accounts.fk3_job_role_id
GROUP BY
job_role_dim.job_role_desc
How can I modify the above code to only display the job_role_desc
that has the maximum number of placements? 如何修改上面的代码以仅显示具有最大放置数量的job_role_desc
? Thank you for your help. 谢谢您的帮助。
You can use row_number()
or dense_rank()
: 您可以使用row_number()
或dense_rank()
:
SELECT ja.*
FROM (SELECT j.job_role_desc AS "Job Role Description" ,
SUM(a.no_of_placements) AS "Number Of Placements",
ROW_NUMBER() OVER (ORDER BY SUM(a.no_of_placements) DESC) as seqnum
FROM fact_accounts a INNER JOIN
job_role_dim j
ON j.job_role_id = a.fk3_job_role_id
GROUP BY j.job_role_desc
) ja
WHERE seqnum = 1;
If you want all versions with the maximum, then use dense_rank()
or rank()
. 如果要使所有版本具有最大值,请使用dense_rank()
或rank()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.