Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Expert,

Need to calculate using the COUNT() for the Whole and for a conditional COUNT()

as depicted below.


name age
-------------
amy 6
pam 8
joe 10
pet 16
jon 2
ana 12


A single Select statement to calculate the percentage of children who are above 10 using the
COUNT() function

in MS SQL SERVER 2008


Thanks

What I have tried:

I am able to calculate them separately, but I need One select statement
Posted
Updated 24-Jan-18 0:56am

Try
SQL
SELECT COUNT(*) AS Total, SUM(CASE WHEN age > 10 THEN 1 ELSE 0 END) * 100 / COUNT(*) AS Percent FROM MyTable
 
Share this answer
 
Select Count (*) * 100 / (Select Count(*) From YourTable) as Score
From YourTable where age>10


Running Copy:SQL Fiddle[^]
Please don't directly copy and paste the sql.
 
Share this answer
 
v6

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