## 2 replies

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

• insafkpm

Member

22 Points

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

• urenjoy

Star

11795 Points

2340 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

94120 Points

18111 Posts

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