为每个类别选择前10条记录我想在一个查询中从每个部分返回前10条记录。有人能帮上忙吗?节是表中的列之一。数据库是SQLServer 2005。我想按输入日期返回前十名。部门是业务,本地和特色。对于某一特定日期,我只需要顶部(10)业务行(最近的条目)、顶部(10)本地行和顶部(10)功能。
3 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
SELECT rs.Field1,rs.Field2 FROM ( SELECT Field1,Field2, Rank() over (Partition BY Section ORDER BY RankCriteria DESC ) AS Rank FROM table ) rs WHERE Rank <= 10
慕少森
TA贡献2019条经验 获得超9个赞
WITH TOPTEN AS ( SELECT *, ROW_NUMBER() over ( PARTITION BY [group_by_field] order by [prioritise_field] ) AS RowNo FROM [table_name])SELECT * FROM TOPTEN WHERE RowNo <= 10
Qyouu
TA贡献1786条经验 获得超11个赞
select *from Things twhere 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 = @Dateorder by Section, DateEntered desc
添加回答
举报
0/150
提交
取消