SQL Server 2008 R2 - grouping the top few

Copyright statement: This article is an original article by the blogger and may not be reproduced without permission  

Please contact me via "Contact Email ([email protected])" in the announcement on the right

Do not use for scholarly citation without author's permission.

Do not use for commercial publication, commercial printing, commercial citation, or other commercial purposes without author's permission.                   

 

This article is revised and improved from time to time. In order to ensure the correctness of the content, it is recommended to read the original text. <---------One day I'm going to make a template myself and kill this potato

Link to this article: http://www.cnblogs.com/wlsandwho/p/4829125.html

Wall of Shame: http://www.cnblogs.com/wlsandwho/p/4206472.html

=======================================================================

Just write a simple example, don't care about asterisks or anything.

copy code
1 USE tempdb
 2
 3 IF EXISTS(SELECT * FROM sysobjects WHERE id=OBJECT_ID(N't_Test') AND OBJECTPROPERTY(id,N'IsUserTable')=1)
 4 DROP TABLE t_Test
 5 GO
 6 CREATE TABLE t_Test(
 7 OnLineDate    DATETIME,
 8 ProductID    NVARCHAR(8),
 9 WebPage        NVARCHAR(32)
10 )
11 GO
12 INSERT INTO t_Test VALUES(GETDATE(),'11111111','1cccccccccc')
13 WAITFOR DELAY '00:00:01'
14 INSERT INTO t_Test VALUES(GETDATE(),'11111111','1eeeeeeeeee')
15 WAITFOR DELAY '00:00:01'
16 INSERT INTO t_Test VALUES(GETDATE(),'11111111','1bbbbbbbbbb')
17 WAITFOR DELAY '00:00:01'
18 INSERT INTO t_Test VALUES(GETDATE(),'11111111','1dddddddddd')
19 WAITFOR DELAY '00:00:01'
20 INSERT INTO t_Test VALUES(GETDATE(),'11111111','1aaaaaaaaaa')
21 WAITFOR DELAY '00:00:01'
22 INSERT INTO t_Test VALUES(GETDATE(),'22222222','2aaaaaaaaaa')
23 WAITFOR DELAY '00:00:01'
24 INSERT INTO t_Test VALUES(GETDATE(),'22222222','2cccccccccc')
25 WAITFOR DELAY '00:00:01'
26 INSERT INTO t_Test VALUES(GETDATE(),'22222222','2eeeeeeeeee')
27 WAITFOR DELAY '00:00:01'
28 INSERT INTO t_Test VALUES(GETDATE(),'22222222','2dddddddddd')
29 WAITFOR DELAY '00:00:01'
30 INSERT INTO t_Test VALUES(GETDATE(),'22222222','2bbbbbbbbbb')
31 GO
32
33 SELECT OnLineDate,ProductID,WebPage,ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY OnLineDate DESC) AS rowRum FROM t_Test
34 GO
35 -----------------------------
36 WITH t_Temp
37 AS
38 (
39 SELECT OnLineDate,ProductID,WebPage,ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY OnLineDate DESC) AS rowRum FROM t_Test
40 )
41 SELECT * FROM t_Temp
42 WHERE t_Temp.rowRum<=3
43 GO
44 -----------------------------
45 WITH t_Temp
46 AS
47 (
48 SELECT OnLineDate,ProductID,WebPage,ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY OnLineDate DESC) AS rowRum FROM t_Test
49 )
50 SELECT * FROM t_Temp
51 WHERE t_Temp.rowRum<=3 ORDER BY ProductID ASC,OnLineDate DESC
52 GO
copy code

=======================================================================

I have nothing to write about recently. Someone in the QQ group asked a question, so I just wrote one.

However, after the person took it, there was no response/feedback.

So I can only say one thing to the vast number of people who reach out to the party. I sincerely hope that you can read the book "Your Knowledge Needs to Manage Tian Zhigang".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325293527&siteId=291194637