问题
I have data that looks as such:
+-----+--------+--------+--------+
| 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 |
+-----+--------+--------+--------+
What I'm trying to do is create another column that provides which score is the highest amongst the three. Remember, I'm not looking for what the value is, I'm looking for the name of the column. Therefore, the final output would look as such:
+-----+--------+--------+--------+------------+
| 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 |
+-----+--------+--------+--------+------------+
I keep trying to do a query involving RANK()
involving PARTITION BY
but it's not giving me what I'm looking for and I'm officially stuck.
回答1:
In Redshift, this might be most easily done using 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, score2, score3) as high_score
from t;
来源:https://stackoverflow.com/questions/58734297/ranking-values-to-determine-highest-value