ID Code
------ --------
1 123
2 123
4 123
6 1234
7 1234
如上是原始表的数据。下面就是要查询出来的效果:
ID Code Row
------- ------- -----------
1 123 1
2 123 2
4 123 3
6 1234 1
7 1234 2
如上是要实现的效果,也就是说把Code字段相同的值,进行自己的行标识。
望高手指教......
------解决方案--------------------
- SQL code
--> 测试数据:[tbl]if object_id('[tbl]') is not null drop table [tbl]create table [tbl]([ID] int,[Code] int)insert [tbl]select 1,123 union allselect 2,123 union allselect 4,123 union allselect 6,1234 union allselect 7,1234--2005以上版本select *,ROW_NUMBER()over(partition by [Code] order by getdate())as row_numfrom 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_num1 123 12 123 24 123 36 1234 17 1234 2*