22,210
社区成员
发帖
与我相关
我的任务
分享
A B C
SZTY 0.00 1
W0006 0.2295 12
W0004 0.2311 15
SZTY 0.00 1
W0004 0.3309 19
W0006 0.3314 22
W0006 0.40 25
SZTY 0.745 28
W0004 0.30 18
能不能得到
SZTY 0.00 1
W0006 0.2295 2
W0004 0.2311 3
SZTY 0.00 1
W0004 0.3309 2
W0006 0.3314 3
W0006 0.40 2
SZTY 0.745 3
W0004 0.30 1
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([A] int,[B] int,[C] int)
insert [tb]
select 1,3,9 union all
select 2,1,6 union all
select 5,7,2
go
select
a,
a排名=(select count(1)+1 from tb where a<t.a),
b,
b排名=(select count(1)+1 from tb where b<t.b),
c,
c排名=(select count(1)+1 from tb where c<t.c)
from
tb t
--测试结果:
/*
a a排名 b b排名 c c排名
----------- ----------- ----------- ----------- ----------- -----------
1 1 3 2 9 3
2 2 1 1 6 2
5 3 7 3 2 1
(所影响的行数为 3 行)
*/
select
a,
a排名=(select count(1)+1 from tb where a<t.a),
b,
b排名=(select count(1)+1 from tb where b<t.b),
c,
c排名=(select count(1)+1 from tb where c<t.c)
from
tb t