博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
每个分类取最新的几条的SQL实现
阅读量:7230 次
发布时间:2019-06-29

本文共 3157 字,大约阅读时间需要 10 分钟。

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) a where 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
 

 

代码

 

    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/05/1639886.html,如需转载请自行联系原作者

你可能感兴趣的文章
如何使用Mybatis第三方插件--PageHelper实现分页操作
查看>>
PyCharm搭建GO开发环境(GO语言学习第1课)
查看>>
Android交互
查看>>
提醒我喝水chrome插件开发指南
查看>>
列表数据转树形数据
查看>>
Java新版本的开发已正式进入轨道,版本号18.3
查看>>
从零开始的webpack生活-0x009:FilesLoader装载文件
查看>>
在electron中实现跨域请求,无需更改服务器端设置
查看>>
gitlab-ci配置详解(一)
查看>>
听说你叫Java(二)–Servlet请求
查看>>
案例分享〡三拾众筹持续交付开发流程支撑创新业务
查看>>
FreeWheel业务系统微服务化过程经验分享
查看>>
移动互联网下半场,iOS开发者如何“高薪”成长?
查看>>
Atlassian是怎样进行持续交付的?且听 Steve Smith一一道来
查看>>
Web Storage相关
查看>>
[PHP内核探索]PHP中的哈希表
查看>>
Apache-drill Architechture
查看>>
WordPress 5.2 Beta 3 发布,要求 PHP 5.6.20 以上版本
查看>>
通通连起来——无处不在的流
查看>>
互联网+时代,看云计算如何改变传统行业
查看>>