最新活动:电脑PC端+手机端+微网站+自适应网页多模板选择-建站388元起价!!!
当前位置:主页 > 网站建设 > 每个分类取最新的几条的SQL实现代码建站知识

每个分类取最新的几条的SQL实现代码建站知识

时间:2023-05-23 11:05:23 阅读: 文章分类: 网站建设 作者: 网站编辑员

导读:1建站知识每个分类取最新的几条的SQL实现代码,需要的朋友可以参考下网站seo优化公司网站建设。

网站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

关键词标签: 代码 SQL 几条

声明: 本文由我的SEOUC技术文章主页发布于:2023-05-23 ,文章每个分类取最新的几条的SQL实现代码建站知识主要讲述几条,代码,SQL网站建设源码以及服务器配置搭建相关技术文章。转载请保留链接: https://www.seouc.com/article/web_5505.html

我的IDC 网站建设技术SEOUC.COM
专注网站建设,SEO优化,小程序设计制作搭建开发定制网站等,数千家网站定制开发案例,网站推广技术服务。
  • 5000+合作客服
  • 8年从业经验
  • 150+覆盖行业
  • 最新热门源码技术文章