1
我已經解決了這個問題,但我只是想知道爲什麼這個工作的方式。我有一個臨時表,我正在從中進行選擇,並且正在尋找顯示一個名稱,與此名稱匹配的記錄數以及該記錄名稱的百分比。這是這樣的我本來它:SQL Server舍入問題尋找解釋
SELECT name, number,
CASE WHEN number = 0 THEN 0 ELSE
convert(Numeric(10,2), number/CONVERT(decimal(5,2),SUM(number)) * 100)
END as "Percentage of Total"
FROM #names
group by name, number
我收到的結果是:
name number Percentage of Total
------------------------- ----------- ---------------------------------------
Test 1 0 0.00
Test 2 22 100.00
Test 3 28 100.00
當我更改查詢到這一點,結果是正確的:
declare @total decimal(5,2)
select @total = SUM(number) FROM #names
SELECT name, number, convert(Numeric(10,2), number/ @total * 100) as "Percentage of Total"
FROM #names
group by name, number
正確結果:
name number Percentage of Total
------------------------- ----------- ---------------------------------------
Test 1 22 44.00
Test 2 0 0.00
Test 3 28 56.00
有人可以解釋發生了什麼,我想更好地理解這一點。謝謝!
Jon
哇感謝您的快速反應和巨大的答案!你能解釋一下OVER()在這個查詢中做什麼?它完美的工作,我只是想了解發生了什麼。 – Jon 2010-01-12 13:59:57