I am trying Query the top N of rows but with a group inside of it inside of OracleSQL
For example:
+-----+----------+----+------+
|JobID|JobTitle |Pay |Name |
+-----+----------+----+------+
|012 |Manager |400 |Bob |
|012 |Manager |400 |Gemma |
|012 |Manager |400 |Ash |
|020 |Supervisor|400 |Dan |
|020 |Supervisor|400 |Hannah|
|013 |Clerk |300 |Sarah |
+-----+----------+----+------+
As from the example above I would like to find all of the employees under a job title and then only show the top 3 most paid jobs and then ordering by pay down.
+-----+----------+----+------+
|JobID|JobTitle |Pay |Name |
+-----+----------+----+------+
|02 |Manager |400 |Bob |
|02 |Manager |400 |Gemma |
|02 |Manager |400 |Ash |
|04 |Supervisor|400 |Dan |
|04 |Supervisor|400 |Hannah|
|03 |Clerk |300 |Sarah |
|01 |Cleaner |200 |Scruff|
|01 |Cleaner |200 |Fry |
+-----+----------+----+------+
Would be the table.
I have tried using a subquery and then adding on rownum <= 3; on the end but still haven't managed to find the desired result.
SELECT * FROM
( SELECT JobID, MAX(Pay) AS Pay, JobTitle, EmpID, Name
FROM Employees,Jobs
WHERE JobID = Employees.Job_ID
GROUP BY JobID, Pay, EmpID, JobTitle,
ORDER BY Pay DESC)
WHERE ROWNUM <= 3;