# calculate the percentage in Query itself RSS

## 2 replies

Last post Aug 15, 2012 01:18 AM by Decker Dong - MSFT

Member

20 Points

120 Posts

### calculate the percentage in Query itself

sir i have a query ..in it am getting two values thatis total attended and right answer

select count(a.useranswer) as tot,(select count(c.useranswer) from Quiz_response as c inner join quiz_answer as d on c.qid =d.qid where c.mailid='nas@g.nbnh' and c.compid=1 and d.status=1 and c.useranswer = d.aid) as Rans from Quiz_response as a inner join quiz_answer as b on a.qid =b.qid where a.mailid='nas@g.nbnh' and a.compid=1 and b.status=1

total attended=tot

i want to calculate the percentage  based on these two values in this  query it self...is it possible?

give mw a solution asap...

Star

12073 Points

1810 Posts

### Re: calculate the percentage in Query itself

Try following:

Select T.tot, T.rans, T.rans*100/T.tot Perc

from

(select count(a.useranswer) as tot,(select count(c.useranswer) from Quiz_response as c inner join quiz_answer as d on c.qid =d.qid where c.mailid='nas@g.nbnh' and c.compid=1 and d.status=1 and c.useranswer = d.aid) as Rans from Quiz_response as a inner join quiz_answer as b on a.qid =b.qid where a.mailid='nas@g.nbnh' and a.compid=1 and b.status=1) T

• ### Decker Dong ...

All-Star

118619 Points

18779 Posts

### Re: calculate the percentage in Query itself

`select count(a.useranswer) as tot,(select count(c.useranswer) as total, (convert(varchar(max),(tot/total)*100)+'%' ) as percentage from Quiz_response as c inner join quiz_answer as d on c.qid =d.qid where c.mailid='nas@g.nbnh' and c.compid=1 and d.status=1 and c.useranswer = d.aid) as Rans from Quiz_response as a inner join quiz_answer as b on a.qid =b.qid where a.mailid='nas@g.nbnh' and a.compid=1 and b.status=1`