I have this query:
select p.UserName, sum(b.PercentRials) as amount, sum(r.Amount) as Pays
from bills b inner join UserProfiles p on b.PayerUserName=p.UserName
left outer join PayReceipts r on p.UserName=r.UserName
where p.[Percent]>0 and b.PayDate>'2014-11-20'
group by p.UserName
I get this error when running it:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
Warning: Null value is eliminated by an aggregate or other SET operation.
I can understand that the outer join causes this error because when I remove the last sum, it runs OK. but I remember doing such queries and getting NULL
for summation on outer join table.
What can I do?
Try this
Type of expression in
SUM
determines the type of the output. So when your sum exceeds the integer limit, you will receive this error. To perceive your data as integer without getting the error, you need tocast
theamount
column to beBIGINT
and therefore the sum will be of typeBIGINT
.