I have an Oracle table with ID, SUBJECT, and PAYLOAD (CLOB). I'd like to get a listing of the TOP 10 records who have the biggest PAYLOAD (LENGTH(PAYLOAD)) grouped by subject. So if I have 10 DISTINCT SUBJECT's in the table, the query should return 100 rows (top 10 per subject).
Asked
Active
Viewed 60 times
0
-
http://stackoverflow.com/questions/134958/get-top-results-for-each-group-in-oracle – Robert Harvey Dec 16 '14 at 19:02
-
I saw that but it doesn't quite work. Need something that evaluates LENGTH of the field, not just an ID. – Domenic D. Dec 16 '14 at 19:11
1 Answers
2
Use row_number()
:
select t.*
from (select t.*, row_number() over (partition by subject order by length(payload) desc) as seqnum
from table t
) t
where seqnum <= 10;

Gordon Linoff
- 1,242,037
- 58
- 646
- 786