SQL Server - 除以列的总和

时间:2015-07-10 21:20:50

标签: sql-server math division

我试图划分两列的总和,并且SQL没有给出十进制的精确数字。请检查下面的查询。我不知道我在这里失踪了什么。

PAYEE和TOTAL的数据类型是十进制(10,2)

查询

SELECT SUM(PAYEES) AS SUM_PAYEES,SUM(TOTAL) AS SUM_TOTAL,SUM(PAYEES)/SUM(TOTAL) AS DIVISION FROM #ADMIN_FEE_1

SUM_PAYEES  SUM_TOTAL   DIVISION
19940.00    59435.00    0.335492

SELECT 19940.00/59435.00 AS DIVISION

DIVISION
0.3354925548

这是SQL Server中的错误吗?有没有办法得到正确的答案? 任何帮助表示赞赏。感谢

2 个答案:

答案 0 :(得分:1)

尝试

SELECT SUM(PAYEES) AS SUM_PAYEES,
SUM(TOTAL) AS SUM_TOTAL,
cast(SUM(PAYEES) as decimal(18,10))/cast(SUM(TOTAL) AS DECIMAL(18,10)) AS DIVISION 
FROM #ADMIN_FEE_1;

EDIT2:

测试了查询

SELECT SUM(19940.00) AS SUM_PAYEES,SUM(59435.00) AS SUM_TOTAL,cast(SUM(19940.00) as decimal(18,10))/
cast(SUM(59435.00) as decimal(18,10)) AS DIVISION

答案 1 :(得分:0)

尝试将其转换为decimal类型,如下所示

SELECT SUM(PAYEES) AS SUM_PAYEES,
SUM(TOTAL) AS SUM_TOTAL,
CAST(SUM(PAYEES)/SUM(TOTAL) AS DECIMAL(12,10)) AS DIVISION 
FROM #ADMIN_FEE_1;