Sql Server Query遇到零除错误

时间:2014-01-15 12:53:06

标签: sql-server-2008

我的查询中出现错误。我怎么解决它。我试过null但是它没有用。

遇到零错误

SELECT ( 100 - ( ( sum(t1.cAmount) - ( SUM(t1.cAmount) * t2.rAdvancedAmotizationPercent / 100 ) ) * 100 ) / t3.rAmount ) AS restofTotalMoblization,
       ( ( sum(t1.cAmount) - ( SUM(t1.cAmount) * t2.rAdvancedAmotizationPercent / 100 ) ) * 100 ) / t3.rAmount           AS totalMoblization
FROM   tblPkgAdvances t1
       INNER JOIN tblFianacialInvoice t2
         ON t1.vsSysPackageId = 'GWSSP/WS/03'
       INNER JOIN tblPkgContractAwardDetails t3
         ON t2.iPackageId = t3.iPackageId
WHERE  t1.vsSysPackageId = 'GWSSP/WS/03'
GROUP  BY t1.cAmount,
          t2.rAdvancedAmotizationPercent,
          t3.rAmount 

2 个答案:

答案 0 :(得分:1)

而不是

(100 - ((sum(t1.cAmount) - (SUM(t1.cAmount) * t2.rAdvancedAmotizationPercent / 100)) * 100) / t3.rAmount)

你可以做到

case when t3.rAmount = 0 
then 0 
else (100 - ((sum(t1.cAmount) - (SUM(t1.cAmount) * t2.rAdvancedAmotizationPercent / 100)) * 100) / t3.rAmount) end

答案 1 :(得分:0)

你可以尝试

SELECT ( 100 - ( ( sum(t1.cAmount) - ( SUM(t1.cAmount) * t2.rAdvancedAmotizationPercent / 100 ) ) * 100 ) / t3.rAmount ) AS restofTotalMoblization,
       ( ( sum(t1.cAmount) - ( SUM(t1.cAmount) * t2.rAdvancedAmotizationPercent / 100 ) ) * 100 ) / t3.rAmount           AS totalMoblization
FROM   tblPkgAdvances t1
       INNER JOIN tblFianacialInvoice t2
         ON t1.vsSysPackageId = 'GWSSP/WS/03'
       INNER JOIN tblPkgContractAwardDetails t3
         ON t2.iPackageId = t3.iPackageId
WHERE  t1.vsSysPackageId = 'GWSSP/WS/03' and t3.rAmount>0
GROUP  BY t1.cAmount,
          t2.rAdvancedAmotizationPercent,
          t3.rAmount 

我刚刚过滤掉大于0的记录