问题
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