Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:08:52+00:00 2026-06-17T16:08:52+00:00

idI have a table like this |—-|———–|——–| | ID | Ethnicity | Gender |

  • 0

idI have a table like this

|----|-----------|--------|
| ID | Ethnicity | Gender |
| 1  |    2      |   1    |
| 2  |    3      |   1    |
| 3  |    4      |   2    |
|----|-----------|--------|
etc ....

And I’m trying to get back a set of results that show me ethnicities group by male(1) and female(2)

So the result row would in this example would be:

Ethnicity    Male    Female
    2         1        0
    3         1        0
    4         0        1

So far I’m close with to what I want with:

SELECT ethnicity, 
(SELECT 
        count(id)
    FROM
        table_name
    WHERE
        gender = '2' ) as female,
(SELECT 
        count(id)
    FROM
        table_name
    WHERE
        gender = '1') as male
FROM table_name
GROUP BY ethnicity

Which gives me:

Ethnicity    Male    Female
2            2        1
3            2        1
4            2        1

But need the count(id) to only be a count of the adno of that ethnicity row if that makes sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:08:53+00:00Added an answer on June 17, 2026 at 4:08 pm

    Try this instead:

    SELECT 
      ethnicity, 
      SUM(CASE WHEN gender = 1 THEN 1 ELSE 0 END) AS female,
      SUM(CASE WHEN gender = 2 THEN 1 ELSE 0 END) AS Male
    FROM students
    GROUP BY ethnicity;
    

    The above should work in virtually any SQL product, not just in MySQL. If you like, however, you can also use this version, which employs MySQL’s implicit conversion of booleans to ints (true -> 1, false -> 0):

    SELECT 
      ethnicity, 
      SUM(gender = 1) AS female,
      SUM(gender = 2) AS Male
    FROM students
    GROUP BY ethnicity;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Using Oracle) I have a table with key/value pairs like this: create table MESSAGE_INDEX
I have a table like this: ID CatID Filename 1 1 abc 2 2
I have one table that looks like this called survey_1: ================================================ |id | token
I have 2 database tables that looks like this Subscriptions table member_id active 5
I have a table that looks like this CREATE TABLE `purchases` ( `id` INT(10)
I have an UPDATE trigger that produces INSERTED and DELETED table like this: INSERTED
I am having trouble trying to get this query right. Say I have a
I have a table which looks like this. +----+--------+---------+-----------+------------------------+ | id | parent |
Hello I have a table structure like this products_id | model_num | master_model_num 1
I have a pg table like this: CREATE TABLE order_status_history ( order_id integer NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.