0

我们应该如何消除 NULL 值而不是将其保留在单独的行中,值必须驻留在列中,因此通过使用执行结果集中的联合集运算符使 NULL 值消失?

考虑简单的 SQL 查询

SELECT
  *
FROM
  (
    (SELECT
      6 + 2 AS addition,
      NULL AS subtraction,
      NULL AS multiplication,
      NULL AS division
    FROM
      DUAL)
    UNION
    (SELECT
      NULL AS addition,
      6 - 2 AS subtraction,
      NULL AS multiplication,
      NULL AS division
    FROM
      DUAL)
    UNION
    (SELECT
      NULL AS addition,
      NULL AS subtraction,
      6 * 2 AS multiplication,
      NULL AS division
    FROM
      DUAL)
    UNION
    (SELECT
      NULL AS addition,
      NULL AS subtraction,
      NULL AS multiplication,
      6 / 2 AS division
    FROM
      DUAL)
  ) A;

实际结果:

+----------+-------------+----------------+----------+
| addition | subtraction | multiplication | division |
+----------+-------------+----------------+----------+
|        8 |        NULL |          NULL  |     NULL |
|     NULL |           4 |          NULL  |     NULL |
|     NULL |        NULL |            12  |     NULL |
|     NULL |        NULL |          NULL  |   3.0000 |
+----------+-------------+----------------+----------+
4 rows in set (0.00 sec)

预期结果:

+----------+-------------+----------------+----------+
| addition | subtraction | multiplication | division |
+----------+-------------+----------------+----------+
|        8 |           4 |          12    |   3.0000 |
+----------+-------------+----------------+----------+
1 row in set (0.00 sec)

SQL 演示

4

2 回答 2

1

你可以试试下面 -

SELECT
  max(addition) as addition,max(subtraction) as subtraction, max(multiplication) as multiplication,max(division) as division
FROM
  (
    (SELECT
      6 + 2 AS addition,
      NULL AS subtraction,
      NULL AS multiplication,
      NULL AS division
    FROM
      DUAL)
    UNION
    (SELECT
      NULL AS addition,
      6 - 2 AS subtraction,
      NULL AS multiplication,
      NULL AS division
    FROM
      DUAL)
    UNION
    (SELECT
      NULL AS addition,
      NULL AS subtraction,
      6 * 2 AS multiplication,
      NULL AS division
    FROM
      DUAL)
    UNION
    (SELECT
      NULL AS addition,
      NULL AS subtraction,
      NULL AS multiplication,
      6 / 2 AS division
    FROM
      DUAL)
  ) A;
于 2019-09-09T06:39:23.783 回答
1

这应该做。

SELECT
 sum(isnull(addition,0)) addition
 ,sum(isnull(subtraction,0)) subtraction
 ,sum(isnull(multiplication,0)) multiplication
 ,sum(isnull(division,0)) division
FROM
  (
    (SELECT
      6 + 2 AS addition,
      NULL AS subtraction,
      NULL AS multiplication,
      NULL AS division
    )
    UNION
    (SELECT
      NULL AS addition,
      6 - 2 AS subtraction,
      NULL AS multiplication,
      NULL AS division
    )
    UNION
    (SELECT
      NULL AS addition,
      NULL AS subtraction,
      6 * 2 AS multiplication,
      NULL AS division
    )
    UNION
    (SELECT
      NULL AS addition,
      NULL AS subtraction,
      NULL AS multiplication,
      6 / 2 AS division
    )
  ) A
于 2019-09-09T07:47:48.603 回答