SQL SERVER中partition的用法

有这样一批数据:

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就是分区,通俗的来讲就是依据某个字段把数据分成区,然后呢,取几个模块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值