sqlserver分组排序取前三条数据

database

 

 

表结构:

 

 

法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后对应数据:

  

 

以上是 sqlserver分组排序取前三条数据 的全部内容, 来源链接: utcz.com/z/533378.html

回到顶部