是否可以检查 select 脚本中的行是否具有相同的值/排名?

Is it possible to check if rows have the same value / rank inside a select script?

我的目标是找到具有 相同排名 的行,并将排名替换为重复项占用的行数。

如果我得到这个结果:

    points  place
1   300         1
2   267         2
3   245         3
4   245         3
5   240         5

我想要这样

    points  place
1   300         1
2   267         2
3   245         3-4
4   245         3-4
5   240         5

那么如何检查行在位置/点上是否具有相同的值,并在围绕它时构建一个案例?到 让它更常见:有没有办法在不给出固定值的情况下检查 2 行是否包含相同的值 价值?所以像 column1 = column1 而不是 column1 = 3

逻辑是使用 window 函数来确定给定 scoreplace 个值的计数。如果有,则不需要连字符:

select points,
       (case when count(*) over (partition by points) = 1
             then place
             else place || '-' || place || count(*) over (partition by points) - 1
        end) as new_place
from t;

这假定 place 是一个字符串——但这可能不是真的。所以,你需要一些转换:

select points,
       (case when count(*) over (partition by points) = 1
             then to_char(place)
             else place || '-' || place || count(*) over (partition by points) - 1
        end) as new_place
from t;

其实min()这个地方是不需要的,因为每一行都是一样的,所以: