2

可能重复:
此 SQL Server 查询除法计算有什么问题?

我在 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)ie 7/48时,它应该给我.14但它给了0

当我这样做时SUM(EnteredACC)*100/SUM(totalCalls),它应该给出14.58(或四舍五入时为15 ),但结果是14。有人可以帮忙吗?

4

3 回答 3

5

答案很简单,很可能是列的数据类型,EnteredACC并且totalCallsINT(或SMALLINTBIGINT等),因此您的结果是INT. 因此,14.58 变为 14,0.14 变为 0。您需要对数据类型进行强制转换(隐式或显式):

SUM(CAST(EnteredACC AS DECIMAL(16,4))/SUM(totalCalls)
于 2012-10-31T17:24:05.130 回答
0

尝试:select cast(cast(7 as decimal(18,2))/cast(48 as decimal(18,2)) as decimal(18,2))

于 2012-10-31T17:22:20.080 回答
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)
于 2012-10-31T17:23:37.147 回答