在一行中找到最大值,并使用最大列名更新新列 [英] find max value in a row and update new column with the max column name

查看:69
本文介绍了在一行中找到最大值,并使用最大列名更新新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的桌子

number  col1   col2   col3   col4  max
---------------------------------------
  0     200    150    300     80         
 16      68    250    null    55        

我想在每一行的col1,col2,col3,col4之间找到最大值,并用最大值列名称更新最后一列"max"!

I want to find max value between col1,col2,col3,col4 in every row and update the last column "max" with the max value column name!

例如,在第一行中,最大值为300,而"max"列的值将为"col3" 结果是这样的:

for example in first row max value is 300 the "max" column value will be "col3" result like this:

number   col1   col2   col3    col4   max
------------------------------------------
  0      200    150    300      80    col3
 16       68    250    null     55    col2

我该怎么做?

推荐答案

查询

SELECT *,(
SELECT MAX(n) 
    FROM
    (
        VALUES(col1),(col2),(col3),(col4)
    ) AS t(n)
)  AS maximum_value
FROM #tmp

这篇关于在一行中找到最大值,并使用最大列名更新新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆