日期:2014-05-18 浏览次数:20691 次
--> 测试数据:[tbl]
if object_id('[tbl]') is not null drop table [tbl]
create table [tbl]([ID] int,[Code] int)
insert [tbl]
select 1,123 union all
select 2,123 union all
select 4,123 union all
select 6,1234 union all
select 7,1234
--2005以上版本
select *,
ROW_NUMBER()over(partition by [Code] order by getdate())as row_num
from tbl
--2000版本
select *,
row_num=(select COUNT(*) from tbl a
where a.Code=b.Code and a.ID<=b.ID) from tbl b
/*
ID Code row_num
1 123 1
2 123 2
4 123 3
6 1234 1
7 1234 2
*