SUM()
SELECT COUNT(*) AS [Records], SUM(t.Amount) AS [Total] FROM dbo.t1 AS t WHERE t.Id > 0 AND t.Id < 101;
이 명령을 실행하면
Arithmetic overflow error converting expression to data type int.
원인이 무엇인지 아십니까?
이 답변 .
답변
INT
max (2,147,483,647), “ COUNT_BIG (*)를 사용합니다.
SELECT COUNT_BIG(*) AS [Records], SUM(t.Amount) AS [Total] FROM dbo.t1 AS t WHERE t.Id > 0 AND t.Id < 101;
SUM
에서 발생하는 경우 Amount
를 .
SELECT COUNT(*) AS [Records], SUM(CONVERT(BIGINT, t.Amount)) AS [Total] FROM dbo.t1 AS t WHERE t.Id > 0 AND t.Id < 101;
답변
이 문제의 원인은 SUM()
함수
t.Amount
를 BIGINT
SELECT COUNT(*) AS [Records], SUM(CAST(t.Amount AS BIGINT)) AS [Total] FROM dbo.t1 AS t WHERE t.Id > 0 AND t.Id < 101;
참조