sqlserver根据分组条件,获取各分组的前N条

select a.* from A as a where typeid=2 and
(select count(*) from A where typeid=2 and ftypeid = a.ftypeid and id > a.id)<8
order by a.ftypeid,a.id desc

 A:表名

A表中有字段:typeid

上面的SQL是根据typeid=2的所有的根据ftypeid来分组,查询出同一个ftypeid的前8条。

 

id:IDENTITY

 

posted @ 2022-03-12 20:31  jiduoduo  阅读(251)  评论(0)    收藏  举报