日期:2014-05-18 浏览次数:20566 次
create table tb(单号 int,条码 varchar(10),送修时间 datetime,维修完成时间 datetime) insert into tb select 1,'A','2011-10-9','2011-10-10' insert into tb select 2,'A','2011-10-15','2011-10-31' insert into tb select 3,'A','2011-12-16','2011-12-30' insert into tb select 4,'B','2011-9-10','2011-9-15' insert into tb select 5,'B','2011-11-10','2011-11-20' insert into tb select 6,'C','2011-12-1','2011-12-3' insert into tb select 7,'C','2011-12-5','2011-12-6' go select row_number()over(partition by 条码 order by 送修时间)序号,* from tb /* 序号 单号 条码 送修时间 维修完成时间 -------------------- ----------- ---------- ----------------------- ----------------------- 1 1 A 2011-10-09 00:00:00.000 2011-10-10 00:00:00.000 2 2 A 2011-10-15 00:00:00.000 2011-10-31 00:00:00.000 3 3 A 2011-12-16 00:00:00.000 2011-12-30 00:00:00.000 1 4 B 2011-09-10 00:00:00.000 2011-09-15 00:00:00.000 2 5 B 2011-11-10 00:00:00.000 2011-11-20 00:00:00.000 1 6 C 2011-12-01 00:00:00.000 2011-12-03 00:00:00.000 2 7 C 2011-12-05 00:00:00.000 2011-12-06 00:00:00.000 (7 行受影响) */ go drop table tb
------解决方案--------------------
新增列时 --> --> (Roy)生成測試數據 if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([单号] int,[条码] nvarchar(1),[送修时间] Datetime,[维修完成时间] Datetime) Insert #T select 1,N'A','2