日期:2014-05-18 浏览次数:20494 次
if OBJECT_ID('tb') is not null Drop table tb; go create table tb(A int, B varchar(14), C int); go insert into tb(A, B, C) select 1, 'A', 5 union all select 1, 'D', 1 union all select 1, 'C', 2 union all select 3, 'XL', 4 union all select 3, 'RL', 6 union all select 3, 'JK', 2 union all select 7, 'U', 4; select t.A,t.B,t.C from ( select rn=ROW_NUMBER() over(partition by A order by A,C) , * from tb ) t order by t.rn, t.A /* A B C ----------- -------------- ----------- 1 D 1 3 JK 2 7 U 4 1 C 2 3 XL 4 1 A 5 3 RL 6 */
------解决方案--------------------
if OBJECT_ID('tb') is not null Drop table tb; go create table tb(A int, B varchar(14), C int); go insert into tb(A, B, C) select 1, 'A', 5 union all select 1, 'D', 1 union all select 1, 'C', 2 union all select 3, 'XL', 4 union all select 3, 'RL', 6 union all select 3, 'JK', 2 union all select 7, 'U', 4; select A, B, C from ( select rn = ( select count(1) from tb b where b.A = a.A and b.C < = a.C ) , a.* from tb a )t order by rn, A