天天看点

SQL运算有NULL导致结果为NULL的解决方法

场景:运算中包含null,导致结果为null。

期望:为null时当0计算。

SQL运算有NULL导致结果为NULL的解决方法

当某字段为null时,赋为0,否则直接运算,结束。

case when 
(select SUM(Amount) from rp_traderbillamount WHERE TraderID=126 AND BrandID=1 and Date>='2021-01-01' and Date<='2021-01-31' and BillType=4)
is null then 0
else (select SUM(Amount) from rp_traderbillamount WHERE TraderID=126 AND BrandID=1 and Date>='2021-01-01' and Date<='2021-01-31' and BillType=4)
end 
           
SQL运算有NULL导致结果为NULL的解决方法