select mysql column based on occurrence and value in another column

nak3c

Did research but still having trouble with this one

suppose I have the table

class     name      score
1         Alex      0       
1         Beth      0
1         Chris     100
1         Dan       90
2         Frank     80
2         George    0
2         Henry     0
3         Jill      90
4         Kerry     0
5         Liam      90
5         Matt      80
5         Nick      0

want to find rows were at least 2 names in the same class have non-zero scores: example output

class     name      score
1         Chris     100
1         Dan       90
5         Liam      90
5         Matt      80

I tried a nested query that first removes the zero scores and then counts the classes reporting where classes > 2 but Im a relative beginner and must be missing something simple.

Tim Biegeleisen

One standard way to handle this query is to use a subquery with conditional aggregation to identify which classes have 2 or more students with non-zero scores. The original table can then be joined to this to obtain your result set.

SELECT t1.class, t1.name, t1.score
FROM scores t1
INNER JOIN
(
    SELECT class,
        SUM(CASE WHEN score > 0 THEN 1 ELSE 0 END) AS scoreCount
    FROM scores
    GROUP BY class
    HAVING scoreCount >= 2
) t2
    ON t1.class = t2.class
WHERE t1.score > 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

If duplicates exist, select the value based on another column

From Dev

mysql - Select unique column based on max value of another column in a different table

From Dev

Summing values from a column based on match in another column and first distinct occurrence of value in a third column

From Dev

Select column datediff value use for another column in same select in mysql

From Dev

MySQL first select column based on parameter value and then value of column

From Dev

MySQL first select column based on parameter value and then value of column

From Dev

MySQL, select column name based on a value

From Dev

mysql select query php based on column value

From Dev

SQL Server - Select column to update based on another column value

From Dev

How to get a value from a column based on second occurrence of a condition after first occurrence of another condition in R?

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

Update value in column based on column count in another table with mysql

From Dev

Select COLUMN name from one table based on VALUE from another in mySQL?

From Dev

Add a column to a dataframe based on another column dealing with multiple occurrence

From Dev

updating column value based on another column value

From Dev

select a column value that matches with a value of another column

From Dev

How to select a bit based on value in another tables column in SQL

From Dev

Select latest value of another column based on a comparison between unique keys

From Dev

Mysql count occurrence by group as a column value

From Dev

MySQL - if value in column then insert value in another column

From Dev

Set value of column based on another column in Postgres?

From Dev

pig to add column based on value in another column

From Dev

selecting a column based on a minimum value of another column

From Dev

Filling a column with NA, based on the value in an another column

From Dev

Updating one column based on the value of another column

From Dev

Python - Access column based on another column value

From Dev

Mandatory column based on the value of another column

From Dev

pandas - change value in column based on another column

Related Related

  1. 1

    SELECT Mysql - Replacing value in one column based on another column

  2. 2

    If duplicates exist, select the value based on another column

  3. 3

    mysql - Select unique column based on max value of another column in a different table

  4. 4

    Summing values from a column based on match in another column and first distinct occurrence of value in a third column

  5. 5

    Select column datediff value use for another column in same select in mysql

  6. 6

    MySQL first select column based on parameter value and then value of column

  7. 7

    MySQL first select column based on parameter value and then value of column

  8. 8

    MySQL, select column name based on a value

  9. 9

    mysql select query php based on column value

  10. 10

    SQL Server - Select column to update based on another column value

  11. 11

    How to get a value from a column based on second occurrence of a condition after first occurrence of another condition in R?

  12. 12

    Update value in a column based on another column in the same table in MYSQL

  13. 13

    Update value in column based on column count in another table with mysql

  14. 14

    Select COLUMN name from one table based on VALUE from another in mySQL?

  15. 15

    Add a column to a dataframe based on another column dealing with multiple occurrence

  16. 16

    updating column value based on another column value

  17. 17

    select a column value that matches with a value of another column

  18. 18

    How to select a bit based on value in another tables column in SQL

  19. 19

    Select latest value of another column based on a comparison between unique keys

  20. 20

    Mysql count occurrence by group as a column value

  21. 21

    MySQL - if value in column then insert value in another column

  22. 22

    Set value of column based on another column in Postgres?

  23. 23

    pig to add column based on value in another column

  24. 24

    selecting a column based on a minimum value of another column

  25. 25

    Filling a column with NA, based on the value in an another column

  26. 26

    Updating one column based on the value of another column

  27. 27

    Python - Access column based on another column value

  28. 28

    Mandatory column based on the value of another column

  29. 29

    pandas - change value in column based on another column

HotTag

Archive