在单行中选择不是空值

时间:2015-10-06 11:06:44

标签: sql sql-server-2008

我有一张如下表:

ID  Value1  Value2  Value3
1   abc      null    null
1   null     def     null
1   null     null    ghi

我想只选择单行中的非空值,即输出应如下所示:

ID  Value1  Value2  Value3
1   abc     def     ghi

是否可以使用sql实现相同的目标?

2 个答案:

答案 0 :(得分:1)

试试这个

select id, 
max(value1) as value1,
max(value2) as value2,
max(value3) as value3
from table
group by id

答案 1 :(得分:1)

SELECT ID,
    MAX(Value1) AS Value1,
    MAX(Value2) AS Value2,
    MAX(Value3) AS Value3
FROM your_table
GROUP BY ID

<强> Demo