Select top 10 records for each category

前端 未结 14 1475
别那么骄傲
别那么骄傲 2020-11-22 04:27

I want to return top 10 records from each section in one query. Can anyone help with how to do it? Section is one of the columns in the table.

Database is SQL Serve

14条回答
  •  生来不讨喜
    2020-11-22 05:06

    This works on SQL Server 2005 (edited to reflect your clarification):

    select *
    from Things t
    where t.ThingID in (
        select top 10 ThingID
        from Things tt
        where tt.Section = t.Section and tt.ThingDate = @Date
        order by tt.DateEntered desc
        )
        and t.ThingDate = @Date
    order by Section, DateEntered desc
    

提交回复
热议问题