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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top