排名值以确定最高价值

我的数据看起来像这样:

+-----+--------+--------+--------+
| ID  | score1 | score2 | score3 |
+-----+--------+--------+--------+
| 123 |     14 |    561 |    580 |
| 123 |    626 |    771 |    843 |
| 123 |    844 |    147 |    904 |
| 456 |    922 |    677 |    301 |
| 456 |    665 |    578 |    678 |
| 456 |    416 |    631 |    320 |
+-----+--------+--------+--------+

我想做的是创建另一列,该列提供三者中最高的分数。记住,我不是在寻找值,而是在寻找列的名称。因此,最终输出将如下所示:

+-----+--------+--------+--------+------------+
| ID  | score1 | score2 | score3 | high_score |
+-----+--------+--------+--------+------------+
| 123 |     14 |    561 |    580 | score3     |
| 123 |    626 |    771 |    843 | score3     |
| 123 |    844 |    998 |    904 | score2     |
| 456 |    922 |    677 |    301 | score1     |
| 456 |    665 |    578 |    678 | score3     |
| 456 |    416 |    631 |    320 | score1     |
+-----+--------+--------+--------+------------+

我一直在尝试进行涉及RANK()的{​​{1}}的查询,但是它没有提供我所需要的信息,因此我被正式卡住了。

qjf6835466qq 回答:排名值以确定最高价值

在Redshift中,使用case最容易做到:

select t.*,(case greatest(score1,score2,score3)
             when score1 then 'score1'
             when score2 then 'score2'
             when score3 then 'score3'
        end) as high_score_name,greatest(score1,score3) as high_score
from t;
本文链接:https://www.f2er.com/3150618.html

大家都在问