【分享】查每个分组前N条记录收藏(转)
from:http://blog.csdn.net/liangCK/archive/2008/11/13/3292540.aspx
自己又对这个有了深的了解,分享下
Code
1---------------------------------
2
3-- Author: liangCK 小梁
4
5-- Title : 查每个分组前N条记录
6
7-- Date : 2008-11-13 17:19:23
8
9---------------------------------
10
11
12
13--> 生成测试数据: #T
14
15IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
16
17CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
18
19INSERT INTO #T
20
21SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
22
23SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
24
25SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
26
27SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
28
29SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
30
31SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
32
33SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
34
35SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
36
37SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
38
39SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'
40
41
42
43--SQL查询如下:
44
45
46
47--按GID分组,查每个分组中Date最新的前2条记录
48
49
50
51--1.字段ID唯一时:
52
53SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)
54
55
56
57--2.如果ID不唯一时:
58
59SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)
60
61
62
63--SQL Server 2005 使用新方法
64
65
66
67--3.使用ROW_NUMBER()进行排位分组
68
69SELECT ID,GID,Author,Title,Date
70
71FROM
72
73(
74
75 SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
76
77 FROM #T
78
79) AS T
80
81WHERE rid<=2
82
83
84
85--4.使用APPLY
86
87SELECT DISTINCT b.*
88
89FROM #T AS a
90
91CROSS APPLY
92
93(
94
95 SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
96
97) AS b
98
99
100
101
102
103--结果
104
105/**//*
106
107
108
109ID GID Author Title Date
110
111---- ----------- ----------------------------- ------------------------- -----------------------
112
113003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
114
115004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
116
117005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
118
119007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
120
121009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
122
123010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000
124
125
126
127(6 行受影响)
128
129*/
130
131
1---------------------------------
2
3-- Author: liangCK 小梁
4
5-- Title : 查每个分组前N条记录
6
7-- Date : 2008-11-13 17:19:23
8
9---------------------------------
10
11
12
13--> 生成测试数据: #T
14
15IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
16
17CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
18
19INSERT INTO #T
20
21SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
22
23SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
24
25SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
26
27SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
28
29SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
30
31SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
32
33SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
34
35SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
36
37SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
38
39SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'
40
41
42
43--SQL查询如下:
44
45
46
47--按GID分组,查每个分组中Date最新的前2条记录
48
49
50
51--1.字段ID唯一时:
52
53SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)
54
55
56
57--2.如果ID不唯一时:
58
59SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)
60
61
62
63--SQL Server 2005 使用新方法
64
65
66
67--3.使用ROW_NUMBER()进行排位分组
68
69SELECT ID,GID,Author,Title,Date
70
71FROM
72
73(
74
75 SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
76
77 FROM #T
78
79) AS T
80
81WHERE rid<=2
82
83
84
85--4.使用APPLY
86
87SELECT DISTINCT b.*
88
89FROM #T AS a
90
91CROSS APPLY
92
93(
94
95 SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
96
97) AS b
98
99
100
101
102
103--结果
104
105/**//*
106
107
108
109ID GID Author Title Date
110
111---- ----------- ----------------------------- ------------------------- -----------------------
112
113003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
114
115004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
116
117005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
118
119007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
120
121009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
122
123010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000
124
125
126
127(6 行受影响)
128
129*/
130
131