我有这个查询:
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
运行时出现此错误:
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.
我可以理解外连接导致此错误,因为当我删除最后一个总和时,它运行正常。但我记得做了这样的查询并在外连接表上求和时得到NULL
。
我能做什么?
最佳答案
试试这个
select p.UserName, sum(b.PercentRials) as amount, sum(CAST (r.Amount AS BIGINT)) 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
SUM 中的表达式类型决定输出的类型。因此,当您的总和超过整数限制时,您将收到此错误。为了将数据视为整数而不出现错误,您需要将 amount
列转换为 BIGINT
,因此总和将为类型BIGINT
。
关于sql-server - SQL : left join and sum: Arithmetic overflow error converting expression to data type int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30698259/