0

我有一个查询,我想在表单中查询,以便在“null”或 0 的情况下我可以用 1 替换它们。

select 
ZEROIFNULL(SUM(CASE WHEN PROCEDURE_TYPE IN (816) and RAT_TYPE_NAME = '2G' THEN (COUNTER_THROUGHPUT_UL) END)) AS COUNTER_THROUGHPUT_UL_2G,
ZEROIFNULL(SUM(CASE WHEN PROCEDURE_TYPE IN (816) and RAT_TYPE_NAME = '3G' THEN (COUNTER_THROUGHPUT_UL) END)) AS COUNTER_THROUGHPUT_UL_3G,
ZEROIFNULL(SUM(CASE WHEN PROCEDURE_TYPE IN (816) and RAT_TYPE_NAME IN ('4G', 'LTE') THEN (COUNTER_THROUGHPUT_UL) END)) AS COUNTER_THROUGHPUT_UL_4G
from Table_name;

在上述查询中,所有列的结果可能为空或为零,我需要将它们替换为 1。我使用 zeroifnull 将所有空值转换为 0,但找不到将 0 转换为的方法1.

我想将所有三列都视为 1。

4

1 回答 1

0

您可以COALESCE()在大多数数据库中使用:

select COALESCE(SUM(CASE WHEN PROCEDURE_TYPE IN (816) and RAT_TYPE_NAME = '2G' THEN (COUNTER_THROUGHPUT_UL) END), 1) AS COUNTER_THROUGHPUT_UL_2G,
       COALESCE(SUM(CASE WHEN PROCEDURE_TYPE IN (816) and RAT_TYPE_NAME = '3G' THEN (COUNTER_THROUGHPUT_UL) END), 1) AS COUNTER_THROUGHPUT_UL_3G,
       COALESCE(SUM(CASE WHEN PROCEDURE_TYPE IN (816) and RAT_TYPE_NAME IN ('4G', 'LTE') THEN (COUNTER_THROUGHPUT_UL) END), 1) AS COUNTER_THROUGHPUT_UL_4G
from Table_name;

我还将通过将常见条件移至WHERE子句来简化查询:

select COALESCE(SUM(CASE WHEN RAT_TYPE_NAME = '2G' THEN COUNTER_THROUGHPUT_UL END), 1) AS COUNTER_THROUGHPUT_UL_2G,
       COALESCE(SUM(CASE WHEN RAT_TYPE_NAME = '3G' THEN COUNTER_THROUGHPUT_UL END), 1) AS COUNTER_THROUGHPUT_UL_3G,
       COALESCE(SUM(CASE WHEN RAT_TYPE_NAME IN ('4G', 'LTE') THEN COUNTER_THROUGHPUT_UL END), 1) AS COUNTER_THROUGHPUT_UL_4G
from Table_name
where PROCEDURE_TYPE IN (816);
于 2019-06-25T18:08:57.833 回答