| 发表于:2007-08-28 20:19:136楼 得分:30 |
--啊,早说啊 --没有主键要借助临时表 declare @test table(日期 varchar(255),编号 varchar(255),项目 varchar(255)) insert @test select '20070101 ', '001 ', 'a ' union all select '20070101 ', '001 ', 'b ' union all select '20070101 ', '001 ', 'a ' union all select '20070101 ', '002 ', 'c ' union all select '20070101 ', '002 ', 'b ' union all select '20070105 ', '003 ', 'd ' union all select '20070106 ', '001 ', 'f ' union all select '20070107 ', '005 ', 'a ' union all select '20070109 ', '001 ', 'g ' union all select '20070109 ', '001 ', 'r ' union all select '20070109 ', '001 ', 'o ' union all select '20070109 ', '001 ', 's ' select orderno = identity(int, 1, 1), * into #temp from @test order by 日期, 编号 select 日期, 编号, id = (select sum(1)-1 from #temp where 日期=a.日期 and 编号=a.编号 and orderno <=a.orderno), 项目 from #temp a /* 20070101 001 0 a 20070101 001 1 b 20070101 001 2 a 20070101 002 0 c 20070101 002 1 b 20070105 003 0 d 20070106 001 0 f 20070107 005 0 a 20070109 001 0 g 20070109 001 1 r 20070109 001 2 o 20070109 001 3 s (所影响的行数为 12 行) */ drop table #temp | | |
|