重复排名

时间:2013-12-13 18:56:02

标签: oracle

我正在使用Oracle Apex而且我正在调整呼叫速度。我几乎得到了我追求的结果。我遇到的唯一问题是,当rank函数遇到重复值时,默认情况下它们都被赋予最低Rank。 e.g。

Rank  Call Speed
1             65
2             72
3             92
4            102
4            102
4            102
4            102
4            102
9            113
10           154
11           201
12           352

无论如何让4的代表为8(重复的最高等级)

这样做的一种方法是使用排名降序,然后从最高等级+1中减去它。这可行,但似乎是不必要的步骤。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:2)

有点奇怪的事情,但我会做的事情如下:

with data as (
    select 65 call_speed from dual union all
    select 72 call_speed from dual union all
    select 92 call_speed from dual union all
    select 102 call_speed from dual connect by level <= 5 union all
    select 113 call_speed from dual union all
    select 154 call_speed from dual union all
    select 201 call_speed from dual union all
    select 352 call_speed from dual
)
select
    rank() over (order by call_speed) + count(*) over (partition by call_speed) - 1 rank,
    call_speed
from data;

这给了你:

      RANK CALL_SPEED
---------- ----------
         1         65
         2         72
         3         92
         8        102
         8        102
         8        102
         8        102
         8        102
         9        113
        10        154
        11        201
        12        352

答案 1 :(得分:2)

只是替代方案,完全没有理由,除了可以避免任何内存开销(?)进行分区计数:

with data as (
    select 65 call_speed from dual union all
    select 72 call_speed from dual union all
    select 92 call_speed from dual union all
    select 102 call_speed from dual connect by level <= 5 union all
    select 113 call_speed from dual union all
    select 154 call_speed from dual union all
    select 201 call_speed from dual union all
    select 352 call_speed from dual
)
select
    count(*) over () + 1 - rank() over (order by call_speed desc) rank,
    call_speed
from data
order by call_speed;