34,587
社区成员
发帖
与我相关
我的任务
分享
select 名称,时间, 数量
from
(select row_number() over(partition by 名称 order by 时间 asc) rn,名称,时间, 数量 from tb) a
where rn<3
---------------------------------
-- Author: liangCK 小梁
-- Title : 查每个分组前N条记录
-- Date : 2008-11-13 17:19:23
---------------------------------
--> 生成测试数据: #T
IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
INSERT INTO #T
SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'
--SQL查询如下:
--按GID分组,查每个分组中Date最新的前2条记录
--1.字段ID唯一时:
SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)
--2.如果ID不唯一时:
SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)
--SQL Server 2005 使用新方法
--3.使用ROW_NUMBER()进行排位分组
SELECT ID,GID,Author,Title,Date
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
FROM #T
) AS T
WHERE rid<=2
--4.使用APPLY
SELECT DISTINCT b.*
FROM #T AS a
CROSS APPLY
(
SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
) AS b
--结果
/*
ID GID Author Title Date
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000
(6 行受影响)
*/
select * from tb a
where 数量 in (select top 10 数量 from tb where a.名称=名称 order by 数量 desc)
create table #temp
(
name varchar(10),
timepoint datetime,
number int
)
go
create table #temp2
(
name varchar(10),
timepoint datetime,
number int
)
go
insert #temp
values('A','2011-08-11',13)
insert #temp
values('A','2011-08-12',14)
insert #temp
values('A','2011-08-12',14)
insert #temp
values('B','2011-08-12',11)
insert #temp
values('B','2011-08-13',235)
insert #temp
values('B','2011-08-13',225)
declare @name varchar(10)
declare cur1 cursor
for select distinct(name) from #temp
open cur1
fetch next from cur1 into @name
while(@@FETCH_STATUS=0)
begin
insert #temp2
select top 2 name,timepoint,number
from #temp
where name=@name
fetch next from cur1 into @name
end
close cur1
deallocate cur1
select *
from #temp2
drop table #temp
drop table #temp2
go
create table #test
(名称 nvarchar(10), 时间 datetime, 数量 int)
insert #test
select 'A','2011-8-11', 13 union all
select 'A','2011-8-12', 14 union all
select 'A','2011-8-12', 16 union all
select 'A','2011-8-12', 11 union all
select 'A','2011-8-12', 16 union all
select 'A','2011-8-12', 44 union all
select 'A','2011-8-12', 11 union all
select 'B','2011-8-12', 11 union all
select 'B','2011-8-13', 235 union all
select 'B','2011-8-13', 225 union all
select 'B','2011-8-13', 245 union all
select 'B','2011-8-13', 255 union all
select 'B','2011-8-13', 275 union all
select 'B','2011-8-13', 285
declare @Temp table(num int identity(1,1),名称 nvarchar(10), 时间 datetime, 数量 int)
insert @Temp(名称 , 时间 , 数量)
select distinct 名称 , 时间 , 数量 from #Test order by 名称,时间 desc,数量 desc
select t1.名称 , t1.时间 , t1.数量 from @Temp as t1 Left join (select min(num) as num,名称 from @Temp group by 名称) as t2
on t1.名称=t2.名称 where t1.num-t2.num+1<=2
--名称 时间 数量
---------- ------------------------------------------------------ -----------
--A 2011-08-12 00:00:00.000 44
--A 2011-08-12 00:00:00.000 16
--B 2011-08-13 00:00:00.000 285
--B 2011-08-13 00:00:00.000 275