【问题标题】:How to find the minimum value in each row along with column name in sql/hive如何在每行中找到最小值以及 sql/hive 中的列名
【发布时间】:2021-06-29 08:19:39
【问题描述】:

例如: 在如下表中:

Id Col1 Col2 Col3 Col4
1 10 29 40 74
2 14 19 41 11
3 9 19 47 77
4 27 79 43 70

我的输出应该返回两列,如下表所示:

Id Col1 Col2 Col3 Col4 MinVal ColumnName
1 10 29 40 74 10 col1
2 14 19 41 11 11 col4
3 9 19 47 77 9 col1
4 27 79 43 70 27 col1

【问题讨论】:

    标签: sql hive


    【解决方案1】:

    你可以使用least():

    select t.*,
           least(col1, col2, col3, col4) as min_val,
           (case least(col1, col2, col3, col4)
                when col1 then 'col1'
                     col2 then 'col2'
                     col3 then 'col3'
                     col4 then 'col4'
            end) as min_val_columnname
    from t;
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2018-01-29
      • 1970-01-01
      • 2020-03-11
      • 2019-07-23
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多