1

My question is similar to the one in How to prevent arithmetic overflow error when using SUM on INT column? but limit the int value.

I want to insert the max limit of the int(2147483647) if the sum(column_name) exceeds the int limit, how to do it? Note: TotalExp datatype is INT

INSERT INTO NEWTABLE
SELECT    UserId,
          SUM( PokemonExp )     AS TotalExp,
          MAX( PokemonLevel )   AS MaxPokeLevel
FROM      mytable
GROUP BY  UserId
ORDER BY  TotalExp DESC
3
  • You will need to cast your data type to bigint Commented Apr 12, 2018 at 11:20
  • 1
    Try replacing your current SUM(PokemonExp) AS TotalExp with CASE WHEN SUM(PokemonExp) >= 2147483647 THEN 2147483647 ELSE SUM(PokemonExp) END AS TotalExp. Note: Pseudo-code. Commented Apr 12, 2018 at 11:39
  • @SchmitzIT when you sum integer columns that would result in a value above 2147483647, you get an overflow error. Which means your suggestion doesn't work. You can try this example: SELECT CASE WHEN sum(a)> 1 THEN 1 ELSE 0 END FROM (values(1),(2147483647)) x(a) Commented Apr 12, 2018 at 12:18

1 Answer 1

0

Convert PokemonExp to bigint

INSERT INTO NEWTABLE
SELECT
  UserId,
  CASE WHEN 
    SUM( CAST(PokemonExp as BIGINT)) > 2147483647
         THEN 2147483647 
         ELSE SUM( CAST(PokemonExp as BIGINT))
         END AS TotalExp,
  MAX( PokemonLevel ) AS MaxPokeLevel
FROM      mytable
GROUP BY  UserId
ORDER BY  TotalExp DESC
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.