如何将计数(案例)除以(案例)()
我正在尝试通过执行以下列
(COUNT(CASE
WHEN col1 > 0
THEN my_id
ELSE null
END)/COUNT(my_id))*100 AS my_percent
My_percent来计算一个%,即输出是所有零的列。
单独计数,返回预期的非阴性整数,几乎所有都是> 0。
COUNT(CASE
WHEN col1 > 0
THEN my_id
ELSE null
END) AS count_case
COUNT(my_id) AS simple_count
%函数为什么返回零而不是正数?如何修改代码以给出预期输出(正数而不是零)?
I am trying to calculate a % by doing the following
(COUNT(CASE
WHEN col1 > 0
THEN my_id
ELSE null
END)/COUNT(my_id))*100 AS my_percent
The column, my_percent, which is output is a column of all zeros.
Individually both COUNTs return non-negative integers as expected, almost all are > 0.
COUNT(CASE
WHEN col1 > 0
THEN my_id
ELSE null
END) AS count_case
COUNT(my_id) AS simple_count
Why does the % function return zeros rather than positive numbers? How can I modify the code to give the expected output (positive numbers not zeros)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
count
有一个bigint
返回值,并且 PostgreSQL 使用整数除法来截断小数位:为避免这种情况,请转换为
双精度
或数字
:count
has abigint
return value, and PostgreSQL uses integer division that truncates fractional digits:To avoid that, cast to
double precision
ornumeric
: