【发布时间】:2014-05-14 19:00:19
【问题描述】:
我试图在 select 语句中划分两列,然后将商四舍五入到小数点后 4 位。
select round(round(sum(case when acct_no = '2999'
and date between '1/1/14' and current_date then amount end)::numeric, 4)::float
/ round(sum(case when acct_no = '3989'
and date between '1/1/14' and current_date then amount end)::numeric, 4)::numeric, 4) column
from table
查询的其余部分将包含多个日期,因此其中的日期应该是必要的。
它给出的错误:
错误:函数round(双精度,整数)不存在
这是在 PostgreSQL 中尝试的。
【问题讨论】:
-
看起来应该可以了!您是否尝试过强制转换而不是 ::numeric,例如: cast(... as numeric) ?此外,您可以尝试将“数量”合并为 0 或其他内容(以防它返回 null)(例如 coalesce(amount,0::expectedtype))
-
目前它正在工作,但唯一的问题是它没有四舍五入。小数点后仍有 14 个数字。
-
那是因为回合需要在除法之后进行。不在单个分子和分母上。
round(round(sum(case when acct_no = '2999' and date between '1/1/14' and current_date then amount end)::numeric, 4)::float / round(sum(case when acct_no = '3989' and date between '1/1/14' and current_date then amount end)::numeric, 4)::Numeric,4)我认为从本质上讲,您的说法是 22.0000 到 4,然后 7.000 到 4,然后将两者除以...3.1428571...但不要四舍五入。 -
哦,我没有意识到问题是数字没有四舍五入,我以为它收到了错误消息。是的,确实,如果你想要四舍五入的结果(@xQbert),你需要在 round() / round() 周围再换一个“round”。
-
好的,现在这是我收到相关错误的时候了。我将在问题中添加另一轮。错误(如前所述)是函数轮(双精度,整数)不存在
标签: sql postgresql rounding