2015-07-10 145 views
0

我想分成兩列的總和和SQL是不給我十進制精確數字。請檢查下面的查詢。我不知道我在這裏錯過了什麼。收款人和TOTAL的SQL Server的 - 分列總和

數據類型是十進制(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中的錯誤?有沒有辦法得到正確的答案? 任何幫助表示讚賞。由於

+1

哪些類型的收款人/總的來說LDS?因爲'sum/sum'版本受到這些字段所要求的任何類型轉換的約束。你的'select 1994'只是分成兩個硬編碼的浮點數,所以你得到一個浮點數。 –

+0

您可以隱式轉換爲類型 - >/CONVERT(SUM(TOTAL)AS DECIMAL(18,2)) –

+0

@MarcB我剛剛更新了問題 –

回答

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; 
+0

這給我的分區爲 - 0.335492000000000000。我想要0.3354925548。 –

+0

嘗試改變精度值 – Rahul

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 
+0

這工作對我來說!謝謝 –

+0

歡迎:) –