Question

I have a Postgres 8.2 table with columns defined as bigint datatype.

When I do sum(column) the return type is numeric. I want to force type cast the summation to be bigint since I know for sure that the result will not exceed the max value of bigint. I was able to achieve this using a subquery like this:

select T.big_col_total::bigint from (select sum(big_col) as big_col_total from big_table) T;

Is there a way to do this without using the subquery, i.e., can I typecast the sum directly somehow? I tried a couple of ways and both result in a syntax error.

(background: I'm running this query from Hibernate so it is important for me the keep the return data type from the query limited to BigInteger. bigint + bigint = numeric (BigDecimal on the Java side)

Was it helpful?

Solution

Just use:

select sum(big_col)::bigint as total 
from big_table

And you should upgrade to a supported version (e.g. 9.x) as soon as possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top