可能重复:
What is wrong with this SQL Server query division calculation?
我在SQL中划分两个值,如
SELECT SUM(totalCalls),
SUM(EnteredBTN),
SUM(EnteredBTN) * 100 / SUM(totalCalls),
SUM(EnteredACC),
SUM(EnteredACC) / SUM(totalCalls),
SUM(SentToTrans),
SUM(SentToTrans) * 100 / SUM(totalCalls),
SUM(Complete_Information),
SUM(Complete_Information) * 100 / SUM(SentToTrans)
现在当我SUM(EnteredACC)/SUM(totalCalls)
即 7/48 时,它应该给我 .14 ,但它会提供 0 。
当我执行SUM(EnteredACC)*100/SUM(totalCalls)
时,它应该提供 14.58 (或四舍五入时 15 ),但结果为 14 。有人可以帮忙吗?
答案 0 :(得分:4)
答案很简单,很可能列EnteredACC
和totalCalls
的数据类型为INT
(或SMALLINT
,BIGINT
等),因此,你的结果是INT
。因此,14.58变为14,0.14变为0.您需要对您的数据类型进行强制转换(隐式或显式):
SUM(CAST(EnteredACC AS DECIMAL(16,4))/SUM(totalCalls)
答案 1 :(得分:0)
尝试:select cast(cast(7 as decimal(18,2))/cast(48 as decimal(18,2)) as decimal(18,2))
答案 2 :(得分:0)
你需要浮点除法,而不是整数除法。
SELECT SUM(totalCalls),
SUM(EnteredBTN),
SUM(EnteredBTN)*100/CAST(SUM(totalCalls) as FLOAT),
SUM(EnteredACC),
SUM(EnteredACC)/SUM(totalCalls),
SUM(SentToTrans),
SUM(SentToTrans) * 100 / SUM(totalCalls),
SUM(Complete_Information),
SUM(Complete_Information) * 100 / CAST(SUM(SentToTrans) as FLOAT)