Bianka Morris
Bianka Morris

Reputation: 31

calculate percentage with group by

I've tried a few ways n not got it I know it's just getting the wording in the right places; it should be easy cos I know how you work out percentages... I'm just struggling to put it down as a query in SQL .

9 people 5 female ,4 male
SELECT sex, count(*),count(*)*100/ count(title)/100.0 as percentage
  FROM details
 GROUP BY sex;

Upvotes: 3

Views: 4121

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

Here is one way to do this using analytic functions:

SELECT sex, COUNT(*), 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS percentage
FROM details
GROUP BY sex;

Upvotes: 5

Related Questions