SQL 服务器:在另一列中显示具有数字的相似行

SQL Server: Show similar rows with a number in another column

我想从 sql 中获取下面给出的第三列。我曾经使用过 DENSE_RANK() 但这并没有达到目的,因为我想要的系列如下:

A 90    1
B 85    2
C 85    2
D 80    4
E 75    5
F 75    5
G 75    5
H 70    8

您可以通过排名实现这一目标:

select t.*,
    RANK() OVER ( ORDER BY t.value  desc) AS rank_id
from(
    select           'A' as id, 90 as value    
    union all select 'B' as id, 85 as value    
    union all select 'C' as id, 85 as value    
    union all select 'D' as id, 80 as value    
    union all select 'E' as id, 75 as value    
    union all select 'F' as id, 75 as value    
    union all select 'G' as id, 75 as value    
    union all select 'H' as id, 70 as value   
) as t

结果:

id  value rank_id
A   90      1
C   85      2
B   85      2
D   80      4
F   75      5
G   75      5
E   75      5
H   70      8