我有以下查询
SELECT DISTINCT pt.incentive_marketing,pt.incentive_channel,pt.incentive_advertising FROM test.pricing pt WHERE pt.contract_id = 90000 group by 1,2,3 order by pt.incentive_marketing;
上述查询返回o / p,如附件所示
但是,我想使用COALESCE将所有空值替换为0
请让我知道如何在上述SELECT查询中实现这一点
SELECT COALESCE( pt.incentive_marketing,'0' ),COALESCE(pt.incentive_channel,'0'),COALESCE( pt.incentive_advertising,'0') FROM test.pricing pt WHERE pt.contract_id = 90000 group by 1,3
其结果如图2所示。
我仍然收到一行空白值
您可以将COALESCE与NULLIF一起使用,以获得一个简短有效的解决方案:
COALESCE( NULLIF(yourField,''),'0' )
如果您的Field等于第二个值(在这种情况下为’),则The NULLIF
function将返回null,使得COALESCE功能在所有情况下都能正常工作:
QUERY | RESULT --------------------------------------------------------------------------------- SELECT COALESCE(NULLIF(null,'0') | '0' SELECT COALESCE(NULLIF('','0') | '0' SELECT COALESCE(NULLIF('foo','0') | 'foo'