好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 每個分類取最新的幾條的SQL實現代碼

每個分類取最新的幾條的SQL實現代碼

熱門標簽:西安青牛防封電銷卡 南京電銷外呼系統運營商 威海智能語音外呼系統 北京辦理400電話多少 山西語音外呼系統價格 智能語音外呼系統哪個牌子好 重慶防封電銷機器人供應商 溫州語音外呼系統代理 400電話申請需要開戶費嗎
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

標簽:新余 黃山 濟寧 中衛 河源 金昌 宜春 貸款群呼

巨人網絡通訊聲明:本文標題《每個分類取最新的幾條的SQL實現代碼》,本文關鍵詞  每個,分類,取最,新的,幾條,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《每個分類取最新的幾條的SQL實現代碼》相關的同類信息!
  • 本頁收集關于每個分類取最新的幾條的SQL實現代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 特级黄一级播放| 西川结衣在线精品视频| 日本深夜影院| 欧美激情自拍| 成人午夜又粗又硬又大| 日本怡春院欧美一区二区三区| 免费a级黄色片| 日韩美女AAAAA片片免费| 自慰喷出来乳白色液体是什么| 色哺乳妇hd| 激情艳女理伦片| 亚洲 欧美 另类 综合 偷拍| 香港AA三级久久三级 | 国产精品尹人在线观看免费| 国产精品免费无遮挡无码永久视频 | 51社区国内在线视频| 被男人吃奶添下面好舒服视频| 欧美五月婷婷| 被男友抱到墙上cao到哭H漫画| 3D动漫精品啪啪一区二区竹菊 | 51黑料网永久入口| 苍井空电影《色欲迷墙》| 欧美久久一区二区| 6漫画下载| 欧美一级特黄特黄做受| 日韩在线视频一区| 爽到无码高潮喷水aV无码网站| 久久久久久精品免费免费看片 | 网爆门事件| 日本a视频在线观看| 啊灬啊灬啊灬快灬高潮的视频| 一级做a爱久久毛片A片Ⅴ无码| 久久精品日本免费线| 中文丰满岳乱妇中文在线观看| 歪歪视频在线看?免费AV| 欧美性高清在线| 黄色毛片a级| 9lPORNY九色9l自拍视频| 亚洲成a人不卡在线观看| 亚洲操| 五月综合丁香|