【问题标题】:SQLite: Maximum amount across the column and return column nameSQLite:跨列的最大数量并返回列名
【发布时间】:2020-09-18 20:26:52
【问题描述】:

我正在努力获取列名以获得跨列的最高测试分数。

我已经设法获得了 max_score

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'

我想要的结果是黄色的。

【问题讨论】:

    标签: sql sqlite select


    【解决方案1】:

    您无需检查所有列对。
    像这样使用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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2013-10-31
      • 1970-01-01
      • 2022-07-26
      相关资源
      最近更新 更多