比率计算在Cloudera

时间:2018-05-01 20:19:17

标签: sql cloudera

如何从具有0,1,NULL之一的变量中获取比率,以确保将NULL计为0并获得所有

的比率
sum(COALESCE(call_received, 0)) AS call_received

我认为上面的代码片段应该处理NULL,我应该将它除以什么以确保我考虑到所有内容?

1 个答案:

答案 0 :(得分:2)

您可以使用COUNT(*)

SELECT SUM(COALESCE(call_received,0)) AS call_received,
       COUNT(*) AS call_total,
       SUM(COALESCE(call_received,0)) / COUNT(*) AS call_received_ratio
FROM yourTable

COUNT(*)在总计数中包含NULL个,因此没有特别要求。