日期:2014-05-18 浏览次数:20496 次
--> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([ID] int,[列] varchar(1),[内容] int) insert [tb] select 1,'A',100 union all select 2,'A',200 union all select 3,'C',300 union all select 4,'B',300 union all select 5,'A',300 union all select 6,'C',200 --------------开始查询-------------------------- --2005/2008 select *,序号=row_number() over(partition by [列] order by id) from [tb] --2000 select *,序号=(select count(1) from tb where [列]=t.[列] and id<=t.id) from [tb] t order by 列,序号 ----------------结果---------------------------- /* ID 列 内容 序号 ----------- ---- ----------- -------------------- 1 A 100 1 2 A 200 2 5 A 300 3 4 B 300 1 3 C 300 1 6 C 200 2 (6 行受影响) */
------解决方案--------------------
select *,序号=row_number() over(partition by 列 order by 内容) from tb