仅当所有值的总和为空时,我才需要将这两个 SUMS 评估为 NULL。
默认情况下会。只需0
根据null
您的ELSE
情况替换。
09:43:30 SYSTEM@dwal> ed
Wrote file S:\spool\dwal\BUFFER_SYSTEM_65.sql
1 with t (x, y) as (
2 select null, 1 from dual union all
3 select null, 1 from dual union all
4 select null, 2 from dual
5 )
6* select sum(x), sum(case y when 1 then x else null end ) from t
09:43:40 SYSTEM@dwal> /
SUM(X) SUM(CASEYWHEN1THENXELSENULLEND)
---------- -------------------------------
Elapsed: 00:00:00.00
09:43:41 SYSTEM@dwal> 2
2* select null, 1 from dual union all
09:43:59 SYSTEM@dwal> c/null/1
2* select 1, 1 from dual union all
09:44:03 SYSTEM@dwal> /
SUM(X) SUM(CASEYWHEN1THENXELSENULLEND)
---------- -------------------------------
1 1
Elapsed: 00:00:00.01