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