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

t2.field1=t1.field1是分组的条件,可自由添加

参考地址:

http://topic.csdn.net/t/20040819/17/3291042.html

 

 

取出数据中每个分类前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)  

 

posted @ 2011-09-14 16:00  Rex.He  阅读(701)  评论(0编辑  收藏  举报