如何从一组列中选择非 NULL 值? [英] How to pick the non-NULL value from a set of columns?
问题描述
我的表格有 1 + 5 列,如下所示.
I have table with 1 + 5 columns as follows.
MemberID ==> 代表 Member_ID
MemberID ==> represents Member_ID
Value1 ==> 代表 Value1
Value1 ==> Represents Value1
Value2 ==> 代表 Value2
Value2 ==> Represents Value2
Value3 ==> 代表 Value3
Value3 ==> Represents Value3
Value4 ==> 代表 Value4
Value4 ==> Represents Value4
Value5 ==> 代表 Value5
Value5 ==> Represents Value5
在任何给定时间只有一列不为空(在 Value1、Value2、Value3、Value4 和 Value5 的集合中).
Only one column is not null at any given time(In the set of Value1, Value2, Value3, Value4 and Value5).
我想运行一个查询,在其中我得到的结果为 Member_ID,Value,其中值是来自 5 个值列中任何一个的非空值.
I want to run a query where in I get the result as Member_ID, Value where the value is non null value from any of the 5 Value columns.
如何在 TSQL 中做到这一点?谢谢,史密斯
How to do that in TSQL? Thank you, Smith
推荐答案
select memberID, coalesce(value1, value2, value3, value4, value5)
from myTable
如果存在所有值都可能为空的可能性,您可能希望默认为一个值.
If the possibility exists that all of the values could be null, you may want to default to a value.
select memberID, coalesce(value1, value2, value3, value4, value5, <default>)
from myTable
这篇关于如何从一组列中选择非 NULL 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!