sqlserver分组排序取前三条数据

 

 

表结构:

 

 

法1:

  select a.* from Table_Test as a where 3 > 

    (select count(*) from Table_Test where Brand_Id = a.Brand_Id and AddTime > a.AddTime )

       order by a.Brand_Id,a.AddTime desc

方法2:

  select * from 

      (select *,ROW_NUMBER() over( partition by Brand_Id order by AddTime desc ) as a from Table_Test) as c 

      where a<4

运行sql后对应数据:

  

 

posted @ 2020-04-28 16:08  Smile灬Lucky  阅读(4638)  评论(0编辑  收藏  举报