MySQL Count values from one column with selecting a value from another column

Suji OK

I have created a table as shown below:

CREATE TABLE myRecord(stu_name VARCHAR(50),gender VARCHAR(50),level VARCHAR(50)); INSERT INTO myRecord VALUES('John','Male','Pre-Primary');
INSERT INTO myRecord VALUES('Jane','Female','Year 1');
INSERT INTO myRecord VALUES('Sue','Female','Year 2');

The expected result is supposed to be the following:

Male | Female | Level
----------------------
1 | 0 | Pre-Primary

But when I run the SQL below:

SELECT SUM(t.Male) AS Male,
SUM(t.Female) AS Female,
t.level
FROM
((SELECT SUM(gender='Male') as Male,
SUM(gender='Female') as Female,
CASE WHEN level='Pre-Primary'
THEN 'Pre-Primary'
END AS level
FROM myRecord
) t)
;

The output is as the following:

Male | Female | Level
----------------------
1 | 2 | Pre-Primary

It also calculated Year 1 and Year 2. I only want it to calculate each gender of the level where level = 'Pre-Primary'. I've tried using GROUP BY level but it gave me the same result. I've also tried where but it gave me an error.

Is there any possible way to do that? I've been working on this for several days but I couldn't solve it and it's kinda disappointing. I hope someone can help on this.

Gordon Linoff

Perhaps you want a where clause:

SELECT SUM(gender = 'Male') AS Male, SUM(gender = 'Female') AS Female, level
FROM myRecord
WHERE level = 'Pre-Primary';

I don't see why a subquery would be necessary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting a table and filtering by several values from one column as well as a value from another

From Dev

SQL - Selecting unique values from one column then filtering based on another

From Dev

SQL - Selecting unique values from one column then filtering based on another

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

mysql insert unique values from one column to a column of another table

From Dev

Selecting distinct value from a column in MySql

From Dev

Selecting data from table where sum of values in a column equal to the value in another column

From Dev

How to get count from distinct value one column in MYSQL

From Dev

Count number of values of one column group by value of another column

From Dev

COUNT values in one Column for Distinct Value in Another Column - EXCEL

From Dev

Aggregating values in one column by their corresponding value in another from two files

From Dev

MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

From Dev

Selecting distinct values from multiple column of a table with their count

From Dev

How to bin data based on values in one column, and count occurrences from another column excluding duplicates in R?

From Dev

Column loop and update another column with COUNT() value from another table

From Dev

Selecting varchar values from a column

From Dev

Count Values From Column

From Dev

Creating a dataframe from another one by column values

From Dev

Copy conditional values from one column to another

From Dev

move values from one column to another

From Dev

Creating a dataframe from another one by column values

From Dev

SQL - Selecting a column from another table twice with different values

From Dev

Replacing values in one column after matching values from another column

From Dev

Get MAX value from one column and MIN from another column

From Dev

Change value from one column into another

From Dev

Replace missing values with a value from another column

From Dev

Need to find value in one column and then return value from another column

From Dev

Copy value from one column based on the value of another column

From Dev

Copy value from one column based on the value of another column

Related Related

  1. 1

    Selecting a table and filtering by several values from one column as well as a value from another

  2. 2

    SQL - Selecting unique values from one column then filtering based on another

  3. 3

    SQL - Selecting unique values from one column then filtering based on another

  4. 4

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  5. 5

    mysql insert unique values from one column to a column of another table

  6. 6

    Selecting distinct value from a column in MySql

  7. 7

    Selecting data from table where sum of values in a column equal to the value in another column

  8. 8

    How to get count from distinct value one column in MYSQL

  9. 9

    Count number of values of one column group by value of another column

  10. 10

    COUNT values in one Column for Distinct Value in Another Column - EXCEL

  11. 11

    Aggregating values in one column by their corresponding value in another from two files

  12. 12

    MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

  13. 13

    Selecting distinct values from multiple column of a table with their count

  14. 14

    How to bin data based on values in one column, and count occurrences from another column excluding duplicates in R?

  15. 15

    Column loop and update another column with COUNT() value from another table

  16. 16

    Selecting varchar values from a column

  17. 17

    Count Values From Column

  18. 18

    Creating a dataframe from another one by column values

  19. 19

    Copy conditional values from one column to another

  20. 20

    move values from one column to another

  21. 21

    Creating a dataframe from another one by column values

  22. 22

    SQL - Selecting a column from another table twice with different values

  23. 23

    Replacing values in one column after matching values from another column

  24. 24

    Get MAX value from one column and MIN from another column

  25. 25

    Change value from one column into another

  26. 26

    Replace missing values with a value from another column

  27. 27

    Need to find value in one column and then return value from another column

  28. 28

    Copy value from one column based on the value of another column

  29. 29

    Copy value from one column based on the value of another column

HotTag

Archive