SQL server 中 取出分组后每组的前几条
表:T
字段:id 自动递增,
field1 数字,
.....
select * from (
select *,(select count(*) from T as t2 where t2.field1=t1.field1 and t2.id <=t1.id) as aaa from T as t1) as t3
where aaa <=2
取出数据中每个分类前5条
select ID,title,D_Picture,addtime,newstypeid,jj,content
FROM news ns
where ID in (select top 5 ID from news where newstypeid = ns.newstypeid and isActive='1' order by addtime desc)
FROM news ns
where ID in (select top 5 ID from news where newstypeid = ns.newstypeid and isActive='1' order by addtime desc)
SELECT * FROM ( SELECT *,SEQ=ROW_NUMBER() OVER (PARTITION BY t1.groupid ORDER BY t1.id) FROM t1) T WHERE SEQ<=10