PostgreSQL sum typecasting as a bigint

一世执手 提交于 2019-12-23 05:22:18

问题


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.

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?


回答1:


The result is obviously bigger than what bigint could hold:

-9223372036854775808 to +9223372036854775807

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

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) ...



回答2:


I solved my problem using following statement

SUM(CAST(gross_amount AS Integer))

This is give the result of the column as SUm bigint,

Note:My column gross_amount was double type.




回答3:


You need to cast it before doing the operation:

SUM(myvalue::bigint)


来源:https://stackoverflow.com/questions/20203081/postgresql-sum-typecasting-as-a-bigint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!