sql - ratio calculated in Cloudera
问题描述
How can I get the ratio from a variable that has one of 0, 1, NULL to make sure the NULLs are counted as 0 and I get the the ratio of all
sum(COALESCE(call_received, 0)) AS call_received
The snippet above I think should take care of the NULLs, what should I divide it by to make sure I take everything into account?
解决方案
您可以使用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
s 在总数中,所以那里没有什么特别的要求。
推荐阅读
- tomcat9 - Certbot 与 Tomcat9 (linode) Ubuntu 19.4
- holoviews - 同步选择 > 1 个图表
- excel - 从今天的日期发送电子邮件
- r - 如何更改 BIG 数据框格式:患者有 2 次就诊,每次就诊有多个变量
- javascript - 根据所选值显示数组中的值(从数组构建字符串)
- hadoop - 为什么 map 任务在 MapReduce 中将其输出写入磁盘?
- javascript - 测试异步- Promise Jest
- laravel - 路由 [/download] 未定义。(看法:
- android - 如何添加请求正文?
- tableau-api - 不同窗格中类别的总数百分比