表 test
name1,name2,shuliang
A a 1
A a 1
A c 2
A c 2
A b 1
A b 1
A b 3
B e 1
B e 1
B f 2
B f 1
B f 1
B d 5
B d 4
希望的结果如下:
这里取top 2
name1,name2,shuliang
A b 5
A c 4
B d 9
B f 4
name1,name2,shuliang
A a 1
A a 1
A c 2
A c 2
A b 1
A b 1
A b 3
B e 1
B e 1
B f 2
B f 1
B f 1
B d 5
B d 4
希望的结果如下:
这里取top 2
name1,name2,shuliang
A b 5
A c 4
B d 9
B f 4
distinct b.*
from
tb a
cross apply
(select top 10 * from tb where name=a.name prder by shuliang desc)b
-- 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 行受影响)
*/
go
--> -->
if not object_id(N'Tempdb..#test') is null
drop table #test
Go
Create table #test([name1] nvarchar(1),[name2] nvarchar(1),[shuliang] int)
Insert #test
select N'A',N'a',1 union all
select N'A',N'a',1 union all
select N'A',N'c',2 union all
select N'A',N'c',2 union all
select N'A',N'b',1 union all
select N'A',N'b',1 union all
select N'A',N'b',3 union all
select N'B',N'e',1 union all
select N'B',N'e',1 union all
select N'B',N'f',2 union all
select N'B',N'f',1 union all
select N'B',N'f',1 union all
select N'B',N'd',5 union all
select N'B',N'd',4
Go
SELECT
[name1],[name2],[shuliang]
FROM
(Select [name1],[name2],SUM([shuliang]) AS [shuliang],ROW_NUMBER()OVER(PARTITION BY [name1] ORDER BY SUM([shuliang]) desc ) AS row
from #test GROUP BY [name1],[name2]
)t
WHERE row<3
/*
name1 name2 shuliang
A b 5
A c 4
B d 9
B f 4
*/
多谢,开始我总是把OVER(PARTITION BY [name1]写成OVER(PARTITION BY [name1],[name2]
所以结果不对。搞不清楚为何不能加name2?
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#test') is null
drop table #test
Go
Create table #test([name1] nvarchar(1),[name2] nvarchar(1),[shuliang] int)
Insert #test
select N'A',N'a',1 union all
select N'A',N'a',1 union all
select N'A',N'c',2 union all
select N'A',N'c',2 union all
select N'A',N'b',1 union all
select N'A',N'b',1 union all
select N'A',N'b',3 union all
select N'B',N'e',1 union all
select N'B',N'e',1 union all
select N'B',N'f',2 union all
select N'B',N'f',1 union all
select N'B',N'f',1 union all
select N'B',N'd',5 union all
select N'B',N'd',4
Go
SELECT
a.[name1],a.[name2],b.[shuliang]
FROM (select DISTINCT [name1],[name2] FROM #test )a
CROSS APPLY
(SELECT TOP 2 [name2],SUM([shuliang]) AS [shuliang] FROM #test WHERE [name1]=a.[name1] GROUP BY [name2] ORDER BY SUM([shuliang]) desc) AS b
WHERE a.name2=b.name2/*
name1 name2 shuliang
A b 5
A c 4
B d 9
B f 4
*/
不可以加[name2],這是合計的排序
if object_ID('test','U') is not null
drop table test
go
create table test
(
name1 varchar(10),
name2 varchar(10),
shuliang int
)
go
insert into test
select 'A','a',1 union all
select 'A','a',1 union all
select 'A','c',2 union all
select 'A','c',2 union all
select 'A','b',1 union all
select 'A','b',1 union all
select 'A','b',3 union all
select 'B','e',1 union all
select 'B','e',1 union all
select 'B','f',2 union all
select 'B','f',1 union all
select 'B','f',1 union all
select 'B','d',5 union all
select 'B','d',4
go
select name1,name2,shuliang from (
select *,row=row_number() over(partition by name1 order by shuliang desc) from (
select distinct name1,name2,shuliang=sum(shuliang) over(partition by name1,name2) from test) a
) b where row between 1 and 2
go
/*
name1 name2 shuliang
---------- ---------- -----------
A b 5
A c 4
B d 9
B f 4(4 行受影响)
*/
和
ROW_NUMBER()OVER
哪种的效率更高呢?
http://www.cnblogs.com/changbluesky/archive/2010/07/19/1780593.html