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
SUM(PokemonExp) AS TotalExpwithCASE WHEN SUM(PokemonExp) >= 2147483647 THEN 2147483647 ELSE SUM(PokemonExp) END AS TotalExp. Note: Pseudo-code.SELECT CASE WHEN sum(a)> 1 THEN 1 ELSE 0 END FROM (values(1),(2147483647)) x(a)