网络编程
位置:首页>> 网络编程>> 数据库>> 每个分类取最新的几条的SQL实现代码

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

 来源:asp之家 发布时间:2012-08-21 10:57:18 

标签:分类,最新的几条

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
投稿

猜你喜欢

  • 我认为在ASP中最好的办法是用编程实现定时刷新Cache,也就是说给Application中储存的设一个过期时间。当然,在ASP中Appli
  • 为了安全我们经常给access数据库加上密码,这样就算让本人下载了数据库,那么想看到数据库中的数据也不是那么简单的,起码多了一层障碍!我们平
  • 在我之前写的几篇网站优化的文章中,着墨最多的是减少HTTP请求。通过减少请求数目,你的浏览器必须能对你的网站所有内容成功检索,总的HTTP请
  •  一个非常简单的将半角"转换为中文"的asp函数function new_str(str) 
  •  新一代W3C,xhtml代码规范,大家在设计网站的时候务必遵循这一规范 ,这将对网站的优化,网站的推广,搜索引擎的友好
  •  写这个文章绝对是偶然的偶然的机会,前年等一回的 元旦节,和 老婆上街 溜达,猛然想起买上一张福利彩票,结果屁都没有中上,开春第一
  • 阅读前:Pro Javascript Techniques翻译连载说明和目录JavaScript的演化是渐进而稳固的。历经过去十年的进程,J
  • SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作,方便自己写SQL时方便一点,想贴上来,一起看看,同时希望
  • 索引 经常要查询的语句,则给它建一个索引 表连接 select T_Oders as o join T_Customers as C on
  • 长期以来一直以为iframe跟div一样都是块级元素,直到今天在一个群中看到一位朋友问到iframe怎么居中的时候,测试了下发现原来我一直对
  • 这个是JS控制图片滚动的效果,当鼠标结果新闻标题时开始滚动到对应的图片,可以作为图片新闻。效果图:<!DOCTYPE HTML PUB
  • Sjoerd Visscher 发现了一个简洁的 方法 让样式在 IE 中作用到未知的元素上——仅需 JS 创建此未知元素即可:docume
  • 直接看代码: 代码如下:Class GoogleTranslator sub Class_Initialize
  • 一般情况下,访问或设置剪贴板,IE 只需使用 window.clipboardData 的 getData 或 setData 方法即可。M
  • 长期以来我就有对几年来交互设计的心得进行总结整理的想法。回到中国来亲身体会到不少同行,主要是交互设计师和视觉设计师对于交互设计的困惑,以及其
  • 这只是个asp小技巧类的东西,它虽然适合在每个不同文件名里调用这个函数,但是也是有前提的,下面让我们来仔细看看其中的原委。  &n
  • Microsoft&reg; SQL Server&trade; 2000 由一系列产品组成,不仅能够满足最大的数据处理系统
  • 在标志设计中文字与图形元素总是紧密配合,只有恰当的字体与图形搭配时才能发挥最大的视觉效果。这也意味着你不能抛开图案,只是一厢情愿地选择你自己
  • overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义
  • 这是 COMSHARP CMS 团队翻译的2009年海外Web设计风潮的第二部分,着重讲解了反 Box 式布局,单页布局,多栏布局,巨型插图
手机版 网络编程 asp之家 www.aspxhome.com