每个分类取最新的几条的SQL实现代码
2023-06-13 09:14:35 时间
CREATETABLEtable1([ID][bigint]IDENTITY(1,1)NOTNULL,[Name][nvarchar](128)NOTNULL,[class]intnotnull,[date]datetimenotnull)class表示分类编号。分类数不固定,至少有上千种分类
date表示该条记录被更新的时间
我们现在想获得每个分类最新被更新的5条记录。
解决方案
selectid,name,class,datefrom(selectid,name,class,date,row_number()over(partitionbyclassorderbydatedesc)asrowindexfromtable1)awhererowindex<=5
createtable#temp
(
companyvarchar(50),
productvarchar(50),
inputDatedatetime
)
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车1","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车2","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车3","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车5","2010-7-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车1","2010-8-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车2","2010-8-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车3","2010-8-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车1","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车2","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车3","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("天津旺旺有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("天津旺旺有限公司","汽车5","2010-8-1")
select*from#temp
createprocgetdata
@numint
as
begin
selecttop4*from
(
select(selectcount(*)from#tempwherecompany=a.companyandproduct<=a.product)as序号,a.company,a.product,a.inputDate
from#tempa
)b
where序号>=@num
orderby序号,inputDatedesc
end
go
getdata2
/*
结果
1杭州大明有限公司汽车12010-08-0100:00:00.000
1北京小科有限公司汽车12010-08-0100:00:00.000
1上海有得有限公司汽车12010-08-0100:00:00.000
1天津旺旺有限公司汽车42010-08-0100:00:00.000
2天津旺旺有限公司汽车52010-08-0100:00:00.000
2上海有得有限公司汽车22010-08-0100:00:00.000
2北京小科有限公司汽车22010-08-0100:00:00.000
2杭州大明有限公司汽车22010-08-0100:00:00.000
3杭州大明有限公司汽车32010-08-0100:00:00.000
3北京小科有限公司汽车32010-08-0100:00:00.000
3上海有得有限公司汽车32010-08-0100:00:00.000
4北京小科有限公司汽车42010-08-0100:00:00.000
4北京小科有限公司汽车42010-08-0100:00:00.000
4上海有得有限公司汽车42010-08-0100:00:00.000
4杭州大明有限公司汽车42010-08-0100:00:00.000
5杭州大明有限公司汽车52010-07-0100:00:00.000
*/
--sql2005
createprocgetdata2005
@numint
as
begin
selecttop4*from
(
selectrow_number()over(partitionbycompanyorderbyproduct)as序号,a.company,a.product,a.inputDate
from#tempa
)b
where序号>=@num
orderby序号,inputDatedesc
end
getdata20054
select*from#temp
select(selectcount(*)from#tempwherecompany+product<=a.company+a.product)as序号,a.company,a.product,a.inputDate
,a.company+a.productas唯一标志一行
from#tempa
orderbycompany,product
复制代码代码如下:
date表示该条记录被更新的时间
我们现在想获得每个分类最新被更新的5条记录。
解决方案
selectid,name,class,datefrom(selectid,name,class,date,row_number()over(partitionbyclassorderbydatedesc)asrowindexfromtable1)awhererowindex<=5
createtable#temp
(
companyvarchar(50),
productvarchar(50),
inputDatedatetime
)
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车1","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车2","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车3","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("杭州大明有限公司","汽车5","2010-7-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车1","2010-8-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车2","2010-8-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车3","2010-8-1")
insertinto#temp(company,product,inputDate)values("北京小科有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车1","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车2","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车3","2010-8-1")
insertinto#temp(company,product,inputDate)values("上海有得有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("天津旺旺有限公司","汽车4","2010-8-1")
insertinto#temp(company,product,inputDate)values("天津旺旺有限公司","汽车5","2010-8-1")
select*from#temp
createprocgetdata
@numint
as
begin
selecttop4*from
(
select(selectcount(*)from#tempwherecompany=a.companyandproduct<=a.product)as序号,a.company,a.product,a.inputDate
from#tempa
)b
where序号>=@num
orderby序号,inputDatedesc
end
go
getdata2
/*
结果
1杭州大明有限公司汽车12010-08-0100:00:00.000
1北京小科有限公司汽车12010-08-0100:00:00.000
1上海有得有限公司汽车12010-08-0100:00:00.000
1天津旺旺有限公司汽车42010-08-0100:00:00.000
2天津旺旺有限公司汽车52010-08-0100:00:00.000
2上海有得有限公司汽车22010-08-0100:00:00.000
2北京小科有限公司汽车22010-08-0100:00:00.000
2杭州大明有限公司汽车22010-08-0100:00:00.000
3杭州大明有限公司汽车32010-08-0100:00:00.000
3北京小科有限公司汽车32010-08-0100:00:00.000
3上海有得有限公司汽车32010-08-0100:00:00.000
4北京小科有限公司汽车42010-08-0100:00:00.000
4北京小科有限公司汽车42010-08-0100:00:00.000
4上海有得有限公司汽车42010-08-0100:00:00.000
4杭州大明有限公司汽车42010-08-0100:00:00.000
5杭州大明有限公司汽车52010-07-0100:00:00.000
*/
--sql2005
createprocgetdata2005
@numint
as
begin
selecttop4*from
(
selectrow_number()over(partitionbycompanyorderbyproduct)as序号,a.company,a.product,a.inputDate
from#tempa
)b
where序号>=@num
orderby序号,inputDatedesc
end
getdata20054
select*from#temp
select(selectcount(*)from#tempwherecompany+product<=a.company+a.product)as序号,a.company,a.product,a.inputDate
,a.company+a.productas唯一标志一行
from#tempa
orderbycompany,product
CodehighlightingproducedbyActiproCodeHighlighter(freeware)http://www.CodeHighlighter.com/-->ifobject_id(N"company")isnotnull
droptablecompany
go
createtablecompany
(
companynamevarchar(2),
productvarchar(60)
)
--公司1
insertintocompany
select"A","A1"union
select"A","A2"union
select"A","A3"union
select"A","A4"union
select"A","A5"union
select"A","A6"union
select"A","A7"union
select"A","A8"union
select"A","A9"union
select"A","A10"
--公司2
insertintocompany
select"B","B1"union
select"B","B2"union
select"B","B3"union
select"B","B4"union
select"B","B5"union
select"B","B6"union
select"B","B7"union
select"B","B8"union
select"B","B9"union
select"B","B10"
--公司3
insertintocompany
select"C","C1"union
select"C","C2"union
select"C","C3"union
select"C","C4"union
select"C","C5"union
select"C","C6"union
select"C","C7"union
select"C","C8"union
select"C","C9"union
select"C","C10"
--公司4
insertintocompany
select"D","D1"union
select"D","D2"union
select"D","D3"union
select"D","D4"union
select"D","D5"union
select"D","D6"union
select"D","D7"union
select"D","D8"union
select"D","D9"union
select"D","D10"
--公司5
insertintocompany
select"E","E1"union
select"E","E2"union
select"E","E3"union
select"E","E4"union
select"E","E5"union
select"E","E6"union
select"E","E7"union
select"E","E8"union
select"E","E9"union
select"E","E10"
--公司6
insertintocompany
select"F","F1"union
select"F","F2"union
select"F","F3"union
select"F","F4"union
select"F","F5"union
select"F","F6"union
select"F","F7"union
select"F","F8"union
select"F","F9"union
select"F","F10"
--公司7
insertintocompany
select"G","G1"union
select"G","G2"union
select"G","G3"union
select"G","G4"union
select"G","G5"union
select"G","G6"union
select"G","G7"union
select"G","G8"union
select"G","G9"union
select"G","G10"
--公司8
insertintocompany
select"H","H1"union
select"H","H2"union
select"H","H3"union
select"H","H4"union
select"H","H5"union
select"H","H6"union
select"H","H7"union
select"H","H8"union
select"H","H9"union
select"H","H10"
--公司9
insertintocompany
select"I","I1"union
select"I","I2"union
select"I","I3"union
select"I","I4"union
select"I","I5"union
select"I","I6"union
select"I","I7"union
select"I","I8"union
select"I","I9"union
select"I","I10"
--公司10
insertintocompany
select"J","J1"union
select"J","J2"union
select"J","J3"union
select"J","J4"union
select"J","J5"union
select"J","J6"union
select"J","J7"union
select"J","J8"union
select"J","J9"union
select"J","J10"
IF(selectObject_id("Tempdb..#t"))ISNULL
selectidentity(int,1,1)asid,*into#tfromcompany
orderbyleft(product,1),cast(substring(product,2,2)asint)
ifobject_id(N"getdata","P")isnotnull
droptablegetdata
go
createprocgetdata
@num1int--第几页
as
begin
selectcompanyname,productfrom
(
selectrow_number()over(partitionbycompanynameorderbyid)as序号,*
from#t
)a
where序号=@num1
orderbycompanyname
end
go
getdata4
go
DROPproceduregetdata
相关文章
- 还在手写SQL实现?试试MyBatis-Plus同款IDEA插件吧!提示太全了,还能一键生成代码!
- 实现SQL Server 原生数据从XML生成JSON数据的实例代码
- MySQL服务器的SQL模式(sql_mode变量)
- MySQL查询:精准搜索SQL语句(mysql查询sql)
- MSSQL导出数据到SQL:实现快速简单的数据迁移(mssql导出sql)
- PostgreSQL快速导入SQL文件(postgresql导入sql文件)
- Linux下实现SQL语句的操作(linux执行sql语句)
- Oracle数据库的高性能SQL优化技术(oracle高性能sql)
- MySQL学习笔记:创建表的SQL语句(mysql创建表的sql)
- MySQL实现分页查询的SQL语句(mysql的分页sql)
- MSSQL,为SQL服务带来更多可能性(sql 服务 mssql)
- 轻松掌握Oracle SQL:教您查看SQL语句(oracle查看sql)
- Oracle SQL跟踪:如何优化数据库性能?(oracle跟踪sql)
- SQL Server导出SQL文件:实现快速数据备份(sqlserver导出sql文件)
- 提升效率:用MySQL查看SQL代码的性能(mysql查看sql执行效率)
- MySQL实现行转列的SQL操作(mysql中sql行转列)
- MySQL 中 SQL 比较详解常用语句及其实现方法(mysql中sql比较)
- 利用Oracle主键SQL优化数据库性能(oracle主键sql)
- Oracle SQL替换指南简单操作轻松有效实现(oracle中sql替换)
- Oracle中使用SQL实现取整(oracle中sql取整)
- Oracle和SQL调试精准定位问题所在(oracle、sql调试)
- 借助Oracle SQL轻松实现除法运算(oracle sql除以)
- Oracle SQL实现求余运算(oracle sql求余)
- Oracle SQL序列有效自动增长及控制ID(oracle sql序列)
- SQL语言查询基础:连接查询联合查询代码
- asp.net(C#)防sql注入组件的实现代码
- Mysql命令行导入sql数据的代码