【发布时间】:2025-11-29 19:05:02
【问题描述】:
我正在做一个整数列的 sum() 并且我想将结果类型转换为一个 bigint - 以避免错误。但是,当我尝试使用 sum(myvalue)::bigint 时,它仍然给我一个超出范围的错误。
我可以对查询做些什么来让它工作吗?还是我必须将列类型更改为 bigint?
【问题讨论】:
-
试试 sum(myvalue::bigint)
-
sum(myvalue)::bigint的意思是“计算聚合函数sum超过myvalue,然后将其结果转换为bigint” - 所以当转换发生时,sum()已经选择了它的结果类型。 -
从文档中,sum 返回
bigint for smallint or int arguments, numeric for bigint arguments, double precision for floating-point arguments, otherwise the same as the argument data type,因此无论如何可能都不需要内部转换。实际上,整数总和溢出 bigint 所需的行数可能首先会在其他方面失败(如磁盘空间、IO 带宽等)。虽然我见过一些实际溢出的边缘案例。
标签: sql postgresql types aggregate-functions bigint