1

I'm not even sure if this is possible or whether I just need to write multiple queries.

I have a database table as follows:-

USER

  • university_id
  • gender

The output I'd like to achieve is as follows

| Uni  | Total Users | Male | Female
| Uni1 | 30          | 15   | 15
| Uni2 | 40          | 25   | 15

I would be grateful if you could let me know if this is possible to achieve in a single queries, or whether I should just use different queries for each result.

Thank you in advance.

Phill

1 Answer 1

3

Here's how you can do it in a single query:

select
  UniversityId,
  count(*) as `Total Users`,
  sum(case when gender = 'male' then 1 else 0 end) as Male,
  sum(case when gender = 'female' then 1 else 0 end) as Female
from User
group by UniversityId

Demo: http://www.sqlfiddle.com/#!2/e7d16a/4

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.