每個分類取最新的幾條的SQL實現代碼建站知識
導讀:1建站知識每個分類取最新的幾條的SQL實現代碼,需要的朋友可以參考下網站seo優化公司網站建設。
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網站seo優化軟件 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
聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-23 ,文章每個分類取最新的幾條的SQL實現代碼建站知識主要講述幾條,代碼,SQL網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_5505.html
- 上一篇:Oracle系統表外鍵的更名建站知識
- 下一篇:PL/SQL 類型格式轉換建站知識