Reputation: 31
I need to limit my results of my query. I need to limit based on the ID not the number of rows.
ex: ID EVENT EVENT_DESC
__ _____ __________
1723 1A 1A desc
1723 1B 1B desc
2214 2A 2A desc
2214 2B 2B desc
I need to get the top 10 ten IDs not the top ten rows. I'm not sure how to do this. Can you point me to some doc that can help me achieve this?
Upvotes: 1
Views: 4780
Reputation: 812
Are you looking for something like this?
SQL Server
SELECT TOP 10 ID FROM TABLE
GROUP BY ID
Oracle
SELECT ID FROM (
SELECT DISTINCT ID FROM TABLE ORDER BY ID
)
WHERE rownum <= 10
Upvotes: 2
Reputation:
SELECT *
FROM (
SELECT id,
event,
event_desc,
rank() over (order by id desc) as rnk
FROM your_table
)
WHERE rnk <= 10
Upvotes: 4