如何从具有0,1,NULL之一的变量中获取比率,以确保将NULL计为0并获得所有
的比率sum(COALESCE(call_received, 0)) AS call_received
我认为上面的代码片段应该处理NULL,我应该将它除以什么以确保我考虑到所有内容?
答案 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
个,因此没有特别要求。