=================================版权声明=================================

版权声明:本文为博主原创文章 未经许可不得转载 

请通过右侧公告中的“联系邮箱(wlsandwho@foxmail.com)”联系我

未经作者授权勿用于学术性引用。

未经作者授权勿用于商业出版、商业印刷、商业引用以及其他商业用途。                   

 

本文不定期修正完善,为保证内容正确,建议移步原文处阅读。                                                               <--------总有一天我要自己做一个模板干掉这只土豆

本文链接:http://www.cnblogs.com/wlsandwho/p/4829125.html

耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html

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

只是写个简单的例子,不要在意星号什么的。

 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

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

最近没什么好写的,QQ群里有人问问题,就随手写了一个。

然而那人拿了之后就再也没有反应/反馈了。

所以只能对广大伸手党说一句,我真心希望你们都能看看《你的知识需要管理 田志刚》这本书。