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 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
复制代码 代码如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if object_id(N'company') is not null
drop table company
go
create table company
(
companyname varchar(2),
product varchar(60)
)
--公司1
insert into company
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
insert into company
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
insert into company
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
insert into company
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
insert into company
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
insert into company
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
insert into company
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
insert into company
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
insert into company
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
insert into company
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 (select Object_id('Tempdb..#t')) IS NULL
select identity(int,1,1) as id,* into #t from company
order by left(product,1),cast(substring(product,2,2) as int)
if object_id(N'getdata','P') is not null
drop table getdata
go
create proc getdata
@num1 int --第几页
as
begin
select companyname,product from
(
select row_number() over (partition by companyname order by id) as 序号,*
from #t
) a
where 序号=@num1
order by companyname
end
go
getdata 4
go
DROP procedure getdata
相关推荐:
SEO大量优化:如何通过精准策略提升网站流量,突破搜索引擎排名瓶颈,吕梁本地网站推广平台
SEO分类:从基础到进阶,全面解析SEO优化的关键要素,新建设网站排名
WPS改写-轻松提升文档创作效率的秘密武器,推广网站的优势
SEO已经成为数字营销的核心,如何利用SEO提升网站流量和转化率,含山网站优化推广
互联网快排:助力企业网站流量爆发的秘密武器,西安网站建设找资源公司
ChatGPT对于大数据发展的帮助:赋能行业变革,推动智能化未来,ai写作真的好吗
SEO关键词的选择步骤:让你的网站在搜索引擎中脱颖而出,ai书法化
ChatGPT安装包Windows版-让智能助手触手可得,q和ai
seo诊断什么意思,seo诊断a5 ,约瑟夫ai
ChatGPT当前不可用?背后的原因与解决方案全解析,ai propos
seo需要懂什么源码,seo需要懂什么源码技术 ,ai 纤维
企业营销:如何在竞争激烈的市场中脱颖而出,美装网站Seo优化
SEO每日:提高网站流量的秘密武器,助你脱颖而出,seo优化和排名技巧
AI提取文章重要内容:让信息抓取更高效、更精准,松鼠ai 教学
ChatPartner无法连接网络?解决方案,让你的聊天体验更顺畅!,如何用ai给自己写作文
seo算是什么营销方式,seo是网络营销吗 ,ai318
seo网络推广要做什么,seo 网络推广 ,ai少女喝药
seo需要学些什么内容,学seo的基础 ,中国ai公司年收入
ChatGPT当前不可用?如何应对AI服务中断的挑战,ai文章免费写作app
seo进阶买什么书运营,seo入门难吗 ,没有ai软件怎么打开ai图片
目前国内最好的AI人工智能软件:未来新篇章
企业如何借助SEO咨询实现精准流量引爆,助力业绩提升,立刻推广的旅游线下营销
AI优化文章:如何利用人工智能提升写作效率和质量
SEO符合:提升网站排名的秘诀,助力企业赢在搜索引擎优化的赛道,优化没续费 网站没了
2024年AI写文章生成器推荐:让创作轻松高效,提升写作水平
SEO深度解析:如何通过深度优化提升网站排名,带来流量和转化,咸宁网站建设大概费用
SEO优化工具优势:提升网站排名,带来流量和转化的秘密武器,ai圆形造字
ChatGPT网页版内容显示不全的原因与解决方案,ai超级绘
AI免费生成文章让创作变得轻松自如
SEO搜索关键词是什么意思?全方位解析关键词优化的核心要素,lol ai图片
SEO薪资这些,你也能月入过万!,天水网站建设公司
ChatGPT页面无法翻阅?带你深度了解背后的原因与解决方案,pdf怎么转换ai文字
什么是seo反连接,网站反链多好还是少好 ,网易ai产品
ChatGPT登录503错误?轻松解决,快速恢复畅享智能体验!,ai怎么加植物材质效果
SEO就是:让你的品牌脱颖而出,获得更多曝光与流量,梅岭关键词排名优化
SEO教你如何快速提升网站排名,打破竞争壁垒!,本溪seo优化排名公司
ChatGPT使用问题?如果您正在使用VPN,请尝试将其关闭,ai制作渐变立体
SEO目的:如何通过精准优化提高网站流量与转化率,百度推广网站关键词
SEO单页网站:助力企业在线营销的一站式解决方案,湖南视频网站优化方式
SEM做得好可以取代SEO吗?浅析两者的异同与未来趋势,王道ai
ChatGPT付款被拒?如何应对与解决常见支付问题,掉包ai
ChatGPT怎么有梯子?突破网络限制,轻松畅享AI智能,ai对唱音响
SEO总计:如何通过SEO优化实现网站流量和排名的飞跃,三元桥网站优化
seo黑帽是什么,列举几种seo黑帽行为 ,穿老款的ai丢人吗
SEO排序:如何让你的网页在搜索引擎中脱颖而出,平谷抖音seo推广招聘
用AI征文工具,轻松创作出精彩文章!
ChatGPT崩一次多久修复?揭秘背后的技术与保障,ai1818818
ChatGPT启动时遇到问题?快速解决方案让你畅享智能对话体验,ai如何保存logo
交友群都有哪些,交友群是干什么的 ,ai猫csgo
怎么用AI生成文章?全新写作方式的揭秘与应用指南