微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL Hive-将空值替换为0Hadoop Hive

如何解决SQL Hive-将空值替换为0Hadoop Hive

进行左联接后,我剩下许多空(空)值。在同一个查询的某些列中,如何将这些空值替换为0?

select 
    m1.*,t2.Apple,t3.Berry,t4.Cherry
from
    table1 as t1
    left join table2 as t2 on t1.id = t2.id
    left join table3 as t3 on t1.id = t3.id
    left join table3 as t4 on t1.id = t4.id
;

示例输出

ID  Apple     Berry    Cheery
1   1         NULL     1
2   1         NULL     NULL
3   NULL      1        NULL
4   NULL      NULL     NULL

解决方法

您可以使用coalesce()null的值替换为0

select 
    t1.*,coalesce(t2.Apple,0) as apple,coalesce(t3.Berry,0) as berry,coalesce(t4.Cherry,0) as cherry
from
    table1 as t1
    left join table2 as t2 on t1.id = t2.id
    left join table3 as t3 on t1.id = t3.id
    left join table4 as t4 on t1.id = t4.id
;

请注意,这假设所有3个水果列都是数字数据类型。

侧面说明:我在原始查询中修复了表别名的一些错字。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。