日期:2014-05-18 浏览次数:20768 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (柜员 nvarchar(4),日期 datetime,时间 time,账号 int,金额 decimal(5,2))
insert into [TB]
select '张三','2012-1-1', '08:35',60135,50.00 union all
select '张三','2012-1-1', '08:36',60125,100.00 union all
select '张三','2012-1-1', '08:37',60139,102.00 union all
select '李四','2012-1-1', '08:35',60235,51.00 union all
select '李四','2012-1-1', '08:35',60335,52.00 union all
select '张三','2012-1-2', '09:35',60335,57.00 union all
select '张三','2012-1-2', '09:45',60335,58.00
select * from [TB]
select [No.]=row_number() over (partition by 柜员,日期 order by 日期,柜员),* 
from TB
/*
No.                  柜员   日期                      时间               账号          金额
-------------------- ---- ----------------------- ---------------- ----------- ---------------------------------------
1                    李四   2012-01-01 00:00:00.000 08:35:00.0000000 60235       51.00
2                    李四   2012-01-01 00:00:00.000 08:35:00.0000000 60335       52.00
1                    张三   2012-01-01 00:00:00.000 08:35:00.0000000 60135       50.00
2                    张三   2012-01-01 00:00:00.000 08:36:00.0000000 60125       100.00
3                    张三   2012-01-01 00:00:00.000 08:37:00.0000000 60139       102.00
1                    张三   2012-01-02 00:00:00.000 09:35:00.0000000 60335       57.00
2                    张三   2012-01-02 00:00:00.000 09:45:00.0000000 60335       58.00
(7 行受影响)
*/