≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > SQL Server

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

人气:408 时间:2019-09-16

这篇文章主要为大家详细介绍了SQL Server每个分类取最新的几条的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 

本文来自:http://www.q1010.com/179/7869-0.html

注:关于SQL Server每个分类取最新的几条的SQL实现代码的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:SQL SERVER

您可能感兴趣的文章

  • SQL Server 数据库主键的生成方式小结(sqlserver,mysql)
  • SQL Server使用cmd命令行窗口操作SqlServer的方法
  • SQLServer 2005 实现数据库同步备份 过程-结果-分析
  • SQL Server CREATE FUNCTION sqlserver用户定义函数
  • sqlserver 查询数据库大小的方法
  • SQL Server 存储过程解密(破解函数,过程,触发器,视图.仅限于SQLSERVER2000)
  • SQLServer 触发器 数据库进行数据备份
  • 用户"sa"登陆失败 SQLServer 错误18456的解决方法
  • SQL Server做购物车系统时利用到得几个sqlserver 存储过程
  • SQLServer 优化SQL语句 in 和not in的替代方案
上一篇:SQL Server SQL Join的一些总结(实例)
下一篇:SQL Server 多列复合索引的使用 绕过微软sql server的一个缺陷
热门文章
  • SQL Server SQL获取第一条记录的方法
  • SQL Server出现System.OutOfMemoryException异常的解决方法
  • SQL Server的 update from 语句的简单示例
  • SQL Server 数据库备份方法菜鸟教程
  • SQL Server 多表关联时在where语句中慎用trim()方法
  • SQL Server数据类型及长度限制详细说明
  • mybaits非配置原因,导致SqlSession was not registered for synchronization异常解析
  • SQL Server 收缩后对数据库的使用有影响吗?
  • SQL Server 格式导致的Excel导入sql出现异常的解决方法
  • SQL Server 连接服务器出现错误 7391的解决方法
  • 最新文章
    • SQL Server存储过程基本语法的简单示例
    • sql查询时增加自动编号和分页的简单示例
    • sql轻松应付百万数据的高效数据分页存储过程的简单示例
    • sql获取一条数据中所有字段的名称和值的实现方法
    • sql分割函数的简单示例
    • SQL Server异常捕获的简单示例
    • SQL SERVER回滚恢复误操作数据的实现方法
    • SQL Server函数或存储过程中抛出异常的实现方法
    • SQL Server创建数据库的完整代码
    • SQL Server创建数据库的命令用法示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。