SQL SERVER中partition的用法

来源:互联网 发布:js星空效果 编辑:程序博客网 时间:2024/04/29 01:22

有这样一批数据:

groupid ModifyDate groupTypeID
----------- ----------------------- -----------
125 2010-12-10 11:51:51.000 1
120 2010-08-27 14:24:49.000 1
120 2010-08-27 14:23:12.000 1
120 2010-08-27 14:11:55.000 1
120 2010-08-27 14:03:23.000 1
125 2010-07-05 14:26:11.000 1
125 2010-07-05 14:07:34.000 1
133 2010-12-30 15:46:37.000 3
118 2010-06-10 17:02:22.000 3
118 2010-06-10 17:00:52.000 3
118 2010-06-10 14:03:35.000 3
118 2010-06-10 14:02:46.000 3
118 2010-04-21 09:56:26.000 3
116 2010-04-09 20:51:58.000 4
116 2010-04-09 20:51:49.000 4

..................................省略

我要去groupTypeID 每个类型的按照时间倒叙的前N条数据,

SQL语句如下:

select * from (select groupid,ModifyDate,groupTypeID,row_number()?
over (partition by groupTypeID order by ModifyDate desc)
as tempRow from t where InitiatorUserCode<>'system')t where tempRow<N?

groupid ModifyDate groupTypeID tempRow
----------- ----------------------- ----------- --------------------
125 2010-12-10 11:51:51.000 1 1
120 2010-08-27 14:24:49.000 1 2
120 2010-08-27 14:23:12.000 1 3
120 2010-08-27 14:11:55.000 1 4
120 2010-08-27 14:03:23.000 1 5
125 2010-07-05 14:26:11.000 1 6
125 2010-07-05 14:07:34.000 1 7
133 2010-12-30 15:46:37.000 3 1
118 2010-06-10 17:02:22.000 3 2
118 2010-06-10 17:00:52.000 3 3
118 2010-06-10 14:03:35.000 3 4
118 2010-06-10 14:02:46.000 3 5
118 2010-04-21 09:56:26.000 3 6
116 2010-04-09 20:51:58.000 4 1
116 2010-04-09 20:51:49.000 4 2

partition就是分区,通俗的来讲就是依据某个字段把数据分成区,然后呢,取几个模块

原创粉丝点击