 # calculate the percentage in Query itself [Answered]RSS

## 2 replies

Last post Aug 14, 2012 09:18 PM by Decker Dong - MSFT

• ### calculate the percentage in Query itself

Aug 13, 2012 03:50 AM|insafkpm|LINK

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...

• ### Re: calculate the percentage in Query itself

Aug 13, 2012 05:03 AM|urenjoy|LINK

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

• ### Re: calculate the percentage in Query itself

Aug 14, 2012 09:18 PM|Decker Dong - MSFT|LINK

Hi,

#### insafkpm

insafkpm

Just use the divide operator to deal with that——

`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`