每个分类取最新的几条的SQL实现代码建站知识
导读:1建站知识每个分类取最新的几条的SQL实现代码,需要的朋友可以参考下网站seo优化公司网站建设。
CREATE TABLE table1( [ID] [bigint] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](128) NOT NULL, [class] int not null, [date] datetime not null)class 表示分类编号。 分类数不固定, 至少有上千种分类 date 表示该条记录被更新的时间 我们现在想获得每个分类最新被更新的5条记录。 解决方案 select id,name,class,date from(select id,name,class,date ,row_number() over(partition by class order by date desc)as rowindex from table1) awhere rowindex <= 5 create table #temp ( company varchar(50), product varchar(50), inputDate datetime ) insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车1','2010-8-1') insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车2','2010-8-1') insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车3','2010-8-1') insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车4','2010-8-1') insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车5','2010-7-1') insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车1','2010-8-1') insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车2','2010-8-1') insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车3','2010-8-1') insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车4','2010-8-1') insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车1','2010-8-1') insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车2','2010-8-1') insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车3','2010-8-1') insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车4','2010-8-1') insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车4','2010-8-1') insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车5','2010-8-1') select * from #temp create proc getdata @num int as begin select top 4 * from ( select ( select count(*) from #temp where company=a.company and product<=a.product) as 序号,a.company,a.product,a.inputDate from #temp a ) b where 序号>=@num order by 序号,inputDate desc end go getdata 2 /* 结果 1 杭州大明有限公司 汽车1 2010-08-01 00:00:00.000 1 北京小科有限公司 汽车1 2010-08-01 00:00:00.000 1 上海有得有限公司 汽车1 2010-08-01 00:00:00.000 1 天津旺旺有限公司 汽车4 2010-08-01 00:00:00.000 2 天津旺旺有限公司 汽车5 2010-08-01 00:00:00.000 2 上海有得有限公司 汽车2 2010-08-01 00:00:00.000 2 北京小科有限公司 汽车2 2010-08-01 00:00:00.000 2 杭州大明有限公司 汽车2 2010-08-01 00:00:00.000 3 杭州大明有限公司 汽车3 2010-08-01 00:00:00.000 3 北京小科有限公司 汽车3 2010-08-01 00:00:00.000 3 上海有得有限公司 汽车3 2010-08-01 00:00:00.000 4 北京小科有限公司 汽车4 2010-08-01 00:00:00.000 4 北京小科有限公司 汽车4 2010-08-01 00:00:00.000 4 上海有得有限公司 汽车4 2010-08-01 00:00:00.000 4 杭州大明有限公司 汽车4 2010-08-01 00:00:00.000 5 杭州大明有限公司 汽车5 2010-07-01 00:00:00.000 */ --sql2005 create proc getdata2005 @num int as begin select top 4 * from ( select row_number() over (partition by company order by product ) as 序号,a.company,a.product,a.inputDate from #temp a ) b where 序号>=@num order网站seo优化软件 by 序号,inputDate desc end getdata2005 4 select * from #temp select ( select count(*) from #temp where company+ product<=a.company+a.product) as 序号,a.company,a.product,a.inputDate ,a.company+a.product as 唯一标志一行 from #temp a order by company,product
声明: 本文由我的SEOUC技术文章主页发布于:2023-05-23 ,文章每个分类取最新的几条的SQL实现代码建站知识主要讲述几条,代码,SQL网站建设源码以及服务器配置搭建相关技术文章。转载请保留链接: https://www.seouc.com/article/web_5505.html
- 上一篇:Oracle系统表外键的更名建站知识
- 下一篇:PL/SQL 类型格式转换建站知识