PostgreSQL和类型转换作为bigint [英] PostgreSQL sum typecasting as a bigint

查看:1832
本文介绍了PostgreSQL和类型转换作为bigint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个整数列的sum(),我想将结果类型转换为bigint,以避免发生错误。但是,当我尝试使用sum(myvalue):: bigint时,它仍然给我超出范围的错误。

I am doing the sum() of an integer column and I want to typecast the result to be a bigint - to avoid an error. However when I try to use sum(myvalue)::bigint it still gives me an out of range error.

我可以对查询执行任何操作来获取这个工作吗?还是我必须将列类型更改为bigint?

Is there anything that I can do to the query to get this to work? Or do I have to change the column type to a bigint?

推荐答案

结果显然比 bigint 可以容纳:

The result is obviously bigger than what bigint could hold:


-9223372036854775808至+9223372036854775807

-9223372036854775808 to +9223372036854775807

Postgres返回 数字

Postgres returns numeric in such a case. You shouldn't have to do anything, it should just work without explicit cast.

如果没有,您可以将基本类型强制转换为 bigint ,从而在任何情况下都将结果强制为数字

If it doesn't, you can cast the base type to bigint, thereby forcing the result to be numeric in any case.

SELECT sum(myvalue::int8) ...

这篇关于PostgreSQL和类型转换作为bigint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆