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
. 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? Thank you for your help.
You can use row_number()
or 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()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.