Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a problem with getting the percentage.

this is my query

 select cid,RecommendToFriend from companyreviews where isrss=0 and cid=98
result is

cid    RecommendToFriend 
98      1
98      1
98      0
98      0
98      1

I want the percentage of Recommendtofriend field.

I have to write like this

select ( sum(RecommendToFriend) /(select Count(RecommendToFriend) from CompanyReviews where IsRss=0 and cid=98))* 100  from companyreviews where IsRss=0 and cid=98


I am not getting the answere.

Please help me.

Thank you......
Posted
Comments
Herman<T>.Instance 13-Jun-14 3:56am    
what is the exact problem? Trying to devide INT fields? Try casting to decimal fields

SQL
select sum(RecommendToFriend)*100.0/count(RecommendToFriend) from CompanyReviews where IsRss=0 and cid=98
 
Share this answer
 
Comments
Debabrata_Das 13-Jun-14 4:12am    
Nice solution! Here 100.0 is actually doing the upcast into a decimal value hence we're getting the output. :)
- DD
Try this:

SQL
SELECT cast(SUM(RecommendToFriend) AS decimal(10,2)) / COUNT(RecommendToFriend)*100
FROM companyreviews
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900