每个分类取最新的几条的SQL实现代码

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

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐