简体   繁体   English

使用以下查询在 hive 中处理除以零错误

[英]handling division by zero error in hive with below query

I have the following statements in hive:我在 hive 中有以下语句:

SELECT
    cardno,
    COALESCE(SUM(CASE WHEN shopcode IN (1191,1228,1225,1521) THEN amount_gbp END), 0) / COALESCE(COUNT(DISTINCT CASE WHEN shopcode IN (1191,1228,1225,1521) THEN amount_gbp END), 0) AS active_cards,
    SUM(col1 + col2 + col3 + col4) / COUNT(*)
FROM
    sales
GROUP BY 
    cardno

I get a "division by zero" error, I cannot use nullif in hive so how can I amend the above to account for this error?我收到一个“被零除”错误,我不能在 hive 中使用 nullif 那么我该如何修改上面的内容来解决这个错误?

First, more recent versions of Hive do support NULLIF() :首先,更新版本的 Hive确实支持NULLIF()

. . . / NULLIF(COUNT(DISTINCT CASE WHEN shopcode  in (1191,1228,1225,1521) THEN amount_gbp END), 0)

If not, you can use a CASE expression:如果没有,您可以使用CASE表达式:

. . . / (CASE WHEN COUNT(DISTINCT CASE WHEN shopcode  in (1191,1228,1225,1521) THEN amount_gbp END) > 0 THEN COUNT(DISTINCT CASE WHEN shopcode  in (1191,1228,1225,1521) THEN amount_gbp END) END)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM