Hi Everyone,
I've solved this issue but I'm just wondering why this works the way it does. I have a temporary table I am selecting from and am looking to display a a name, the number of records that match this name, and the percentage for that name of the total records. This is the way I originally had it:
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
The results I received were:
name number Percentage of Total
------------------------- ----------- ---------------------------------------
Test 1 0 0.00
Test 2 22 100.00
Test 3 28 100.00
When I change the query to this, the results are correct:
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
Correct Results:
name number Percentage of Total
------------------------- ----------- ---------------------------------------
Test 1 22 44.00
Test 2 0 0.00
Test 3 28 56.00
Can someone explain what is going on, I would like to understand this better. Thanks!
Jon