好好爱自己!

PostgreSQL sum typecasting as a bigint

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

 

posted @ 2017-08-22 15:17  立志做一个好的程序员  阅读(479)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处