我试图在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查询的其余部分将包含多个日期,因此其中的日期应该是必需的。
它所造成的错误:
错误:函数圆(双精度,整数)不存在。
这是在PostgreSQL中尝试的。
发布于 2014-05-14 19:48:16
我重新格式化了您的示例代码,以尝试更容易地理解它:
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问题是,您正在将除法操作的分子转换为float数据类型double precision。
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因此,表达式的结果是一个double precision值,而不是numeric值,因此出现了您观察到的错误。
发布于 2014-05-14 20:49:56
SELECT round((
sum(CASE WHEN acct_no = '2999'
AND thedate between '2014-1-1' AND current_date THEN amount END)
/ sum(CASE WHEN acct_no = '3989'
AND thedate between '2014-1-1' AND current_date THEN amount END)
)::numeric, 4) AS result
FROM tbl;round()。只对numeric,根据文件。numeric,得到double precision (float8)。测试:
选择5::浮动/3::数值--结果是双精度date作为列名。它是标准SQL中的保留字,在Postgres中是基类型。如果不是因为您提到的rest of the query,这可以进一步简化:
SELECT round(( sum(CASE WHEN acct_no = '2999' THEN amount END)
/ NULLIF(sum(CASE WHEN acct_no = '3989' THEN amount END), 0)
)::numeric, 4) AS result
FROM tbl
WHERE thedate between '2014-1-1'::date AND current_date;最后,在除数上使用NULLIF()捕获“除法为0”的异常。
https://stackoverflow.com/questions/23662963
复制相似问题