anon
anon

Reputation: 1117

Oracle SQL: Show n rows for each id

I have a table with 2 columns, ID and URLS, for each ID there are several URLS. I want to extract only 5 maximum URLS per ID. How do I go about doing this in Oracle SQL?

Upvotes: 2

Views: 1741

Answers (1)

John Woo
John Woo

Reputation: 263733

Try this one,

SELECT ID, URL
FROM
    (
        SELECT ID, URL,
               ROW_NUMBER() OVER (PARTITION BY ID ORDER BY URL DESC) RN
        FROM   tableName
    ) a
WHERE RN <= 5

SQLFiddle Demo

Upvotes: 7

Related Questions