https://stackoverflow.com/questions/20203081/postgresql-sum-typecasting-as-a-bigint
Question:
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?
------------------
Answer:
Try sum(myvalue::bigint)
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) ...