SQL latest/top items in category

Posted by drozzy on Stack Overflow See other posts from Stack Overflow or by drozzy
Published on 2010-04-01T15:55:33Z Indexed on 2010/04/01 16:03 UTC
Read the original article Hit count: 298

Filed under:
|

What is a scalable way to select latest 10 items from each category.

I have a schema list this:

item   category   updated

so I want to select 10 last update items from each category. The current solution I can come up with is to query for categories first and then issue some sort of union query:

query = none
for cat in categories:
   query += select top 10 from table where category=cat order by updated

I am not sure how efficient this will be for bigger databases (1 million rows). If there is a way to do this in one go - that would be nice.

Any help appreciated.

© Stack Overflow or respective owner

Related posts about sql

Related posts about hsql