sqlserver分组排序取前三条数据
程序员文章站
2022-03-26 19:48:35
表结构: 方法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. ......
表结构:
方法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巧用row_number和partition by分组取top数据
-
sqlserver巧用row_number和partitionby分组取top数据(教程)
-
sqlserver分组排序取前三条数据
-
sql数据库怎么实现分组并取每组的前1条语句,按日期排序?
-
sqlserver巧用row_number和partition by分组取top数据
-
db2 分组前几条数据,分组取最后几条数据 db2
-
db2 分组前几条数据,分组取最后几条数据 db2
-
MySQL分组排序取前N条记录以及生成自动数字序列--groupby后limit外加rownumber_MySQL
-
MySQL分组排序取前N条记录以及生成自动数字序列的SQL_MySQL
-
MySQL分组排序取前N条记录以及生成自动数字序列--groupby后limit外加rownumber_MySQL