sqlite:列和返回列名之间的最大值

brtdzjyr  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(235)

我正在努力获得列的最大测试分数的列名。
我总算拿到了满分

select *, max(Test_1, Test_2, Test_3) as max_score
from result_table;

但我需要的是一个列名。我有很多列要比较,所以我想避免使用

case when (Test_1 > Test_2) and (Test_1 > Test_3) then 'Test_1'

我想要的结果是黄色的。

s8vozzvw

s8vozzvw1#

您不需要检查所有列对。
使用 CASE 表达方式如下:

select *, 
  case max(Test_1, Test_2, Test_3)
    when Test_1 then 'Test_1'
    when Test_2 then 'Test_2'
    when Test_3 then 'Test_3'
  end as max_score
from result_table;

或:

select *, 
  'Test_' ||
  case max(Test_1, Test_2, Test_3)
    when Test_1 then 1
    when Test_2 then 2
    when Test_3 then 3
  end as max_score
from result_table;

相关问题