Skip to content Skip to sidebar Skip to footer

Select First N Records For Each Distinct Id In Sql Server 2008

I'm trying to create a query that will pull the first 10 records of each id in a table. Something like this sounds: select distinct top 2 id, column1 from table group by id, colum

Solution 1:

ROW_NUMBER() is very useful for this type of thing.

http://msdn.microsoft.com/en-us/library/ms186734.aspx

SELECT*FROM (
SELECTROW_NUMBER() OVER(PARTITIONBY ID ORDERBY ID) as RowNum, 
    ID, 
    Column1
FROMTable
) MyData
WHERE RowNum <10

Post a Comment for "Select First N Records For Each Distinct Id In Sql Server 2008"