SQL:包含 NULLS 的两列之和?

SQL: Sum of two columns containing NULLS?

我想将两列相加得到第三列。如果一行对两列都有 NULL,我希望第三列有 NULL,而不是 0。原因是因为我想获得第三列的平均值并且 0 会改变该平均值。

| Value1 | Value2 | Value3 |
|--------|--------|--------|
| 10     | NULL   | 10     |
| 20     | NULL   | 20     |
| NULL   | NULL   | NULL   |
| 20     | 30     | 50     |
| NULL   | NULL   | NULL   |

我尝试了以下方法,但它们都为 NULL 行提供了 0。实现此目标的最佳方法是什么?

CREATE TABLE #AMOUNTS (
    Value1 INT,
    Value2 INT
)

INSERT INTO #AMOUNTS VALUES
(10, NULL), (20, NULL), (NULL, NULL), (20, 30), (NULL, NULL)

SELECT 
Value1, Value2

,ISNULL(Value1, 0) + ISNULL(Value2, 0) AS Value3
,COALESCE(Value1, 0) + COALESCE(Value2, 0) AS Value3
,CASE WHEN Value1 = NULL AND Value2 = NULL THEN NULL
    ELSE ISNULL(Value1, 0) + ISNULL(Value2, 0)
END AS Value3
FROM #AMOUNTS
| Value1 | Value2 | Value3 | Value3 | Value3 |
|--------|--------|--------|--------|--------|
| 10     | NULL   | 10     | 10     | 10     |
| 20     | NULL   | 20     | 20     | 20     |
| NULL   | NULL   | 0      | 0      | 0      |
| 20     | 30     | 50     | 50     | 50     |
| NULL   | NULL   | 0      | 0      | 0      |

测试 NULL 时使用运算符 IS 而不是 =:

CASE WHEN Value1 IS NULL AND Value2 IS NULL THEN NULL
ELSE ISNULL(Value1, 0) + ISNULL(Value2, 0)

使用case表达式:

select (case when value1 is null and value2 is null
             then null
             else coalesce(value1, 0) + coalesce(value2, 0)
        end)

也就是说,apply 是获得平均值的简单方法:

select t.*, v.avg_value
from t cross apply
     (select avg(v.val) as avg_value
      from (values (value1), (value2)) v(val)
     ) v;
         

如果您不想在结果列中显示 0,您可以使用 ISNULL()NULLIF():

SELECT *, NULLIF(ISNULL(Value1, 0) + ISNULL(Value2, 0), 0) Value3
FROM Amounts

参见demo