August 2, 2013 at 6:46 pm
I'm trying to use count to figure the percentage of the total number in a column.
DECLARE @total_count AS int
SET @total_count =
(SELECT COUNT (*) COD
FROM [dbo].[1870_1880_DAT])
DECLARE @cuase_count AS int
SET @cuase_count =
(SELECT COUNT(*) COD
FROM [dbo].[1870_1880_DAT]
WHERE [COD] = 'Pneumonia')
SELECT @cuase_count/@total_count As [Percent]
August 2, 2013 at 7:06 pm
I just tested these two statements outputs. The output was correct. The SELECT @cuase_count/@total_count As [Percent] failed.
August 2, 2013 at 7:13 pm
Use NV_morality_Study
Go
DECLARE @total_count AS int
SET @total_count =
(SELECT COUNT (*) COD
FROM [dbo].[1870_1880_DAT])
SELECT @total_count AS [total count]
Returns 1355
DECLARE @cause_count AS int
SET @cause_count =
(SELECT COUNT(*)
FROM [dbo].[1870_1880_DAT]
WHERE [COD] = 'Pneumonia')
SELECT @cause_count AS [cuase count]
Returns 189
SELECT @cause_count / @total_count As [Percent]
Returns 0
August 3, 2013 at 1:35 pm
You declared both variables as an integer. So the result of the division is converted to an integer too.
189/1355 = 0,13948....
This value converted to an integer = 0
Try declaring your variables not as an integer but as, for example, a decimal(10,5) and see what happens.
Read the section Result Types in the books online here
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply