November 3, 2012 at 2:19 am
for example there iam having table @counttable table in i want to caluculate percentage
by this to two column countvalue ,Answer eg; percentage= (,countvalue / Answer)*100
this is the table
DECLARE @counttable table
(
countid int identity(1,1),
contactid int,
countvalue int,
TotalRespondent int,
Answer int
)
insert into @counttable
select 1,1,3,2 union all
select 1,2,3,2 union all
select 1,1,3,2 union all
select 1,1,3,2 union all
select 2,1,3,1 union all
select 2,1,3,1 union all
select 2,1,3,1 union all
select 3,0,3,0 union all
select 3,0,3,0 union all
select 3,0,3,0
i tried three different ways
FIRST TYPE
select
countid
,contactid
,countvalue
,TotalRespondent
, Answer
, COALESCE(countvalue/NULLIF(Answer,0), 0) as 'QuestionPercentage'
from @counttable
order by contactid
SECOND TYPE
SELECT
countid
,contactid
,countvalue
,TotalRespondent
, Answer
,countvalue / (Answer-ABS(SIGN(Answer))+1) as 'QuestionPercentage'
from
@counttable
order by contactid
IN THE THIRD TYPE LIKE THIS
select
countid
,contactid
,countvalue
,TotalRespondent
, Answer
,(CAST( ((CONVERT(DECIMAL(38,2),countvalue) /cast((Answer)as float))*100)as decimal(10,2)))as QuestionPercentage
from @counttable
order by contactid
WITH ERROR LIKE THIS
Msg 8134, Level 16, State 1, Line 21
Divide by zero error encountered.
iam trying to receive output like this
countidcontactid countvalue TotalRespondentAnswerQuestionPercentage
1 1 1 3 2 50.00
2 1 2 3 2 100.00
3 11 3 2 50.00
4 11 3 250.00
5 21 3 1100.00
6 21 3 1100.00
7 21 3 1100.00
8 30 3 00.00
9 30 3 00.00
10 30 3 00.00
can any one plz try to solve my problem
November 3, 2012 at 4:07 am
Hi sivaji,
try this :
select *, case countvalue when 0 then 0 else floor(((countvalue*1.0)/(answer*1.0))*100) end from @counttable order by countID
let me know result
kunal.
November 3, 2012 at 4:57 am
hi KcV
it was working good when count value is 0 and answer value is 0
but it make error when it was like this
DECLARE @counttable table
(
countid int identity(1,1),
contactid int,
countvalue int,
TotalRespondent int,
Answer int
)
insert into @counttable
select 3,0,3,0 union all
select 3,1,3,0 union all
select 3,0,3,1
select *, QuestionPercentage=case countvalue when 0 then 0 else floor(((countvalue*1.0)/(answer*1.0))*100 )
end from @counttable order by countID
November 3, 2012 at 12:35 pm
sivajii (11/3/2012)
hi KcVit was working good when count value is 0 and answer value is 0
but it make error when it was like this
DECLARE @counttable table
(
countid int identity(1,1),
contactid int,
countvalue int,
TotalRespondent int,
Answer int
)
insert into @counttable
select 3,0,3,0 union all
select 3,1,3,0 union all
select 3,0,3,1
select *, QuestionPercentage=case countvalue when 0 then 0 else floor(((countvalue*1.0)/(answer*1.0))*100 )
end from @counttable order by countID
So look at the problem and fix it. The only thing that can give you the error you're getting is when "answer" is zero. Look at the problem and decide what you want the aswer to be if "answer = 0" and then tweak the formula to make the error go away.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply