Select all columns greater than some value

Dexter

I am new to mysql and I have structure like this

timestamp | Name | Total | Student1 | Student2 | Student3
20150911  | X    | 100   |       76 |       10 |       NA
20151111  | Y    | 70    |       NA |        0 |       23

Now I want to select with mysql query where at given 'timestamp'

  • all student columns which have non-zero numbers
  • all student columns which are NA
  • all student columns which are zero

Expected outcome

  • If I put 'non-zero' query , it should give names of column as ' Student1, Student2' for timestamp 20150911
  • If I put 'zero' query , it should give names of column as () for timestamp 20150911
  • If I put 'NA' query, it should give names of column as 'Student3' for timestamp 20150911

I have tried searching for such mixed selection but so far I am not successful. Most of answers are concentrating on row selection rather columns.

Lukasz Szozda

Your data structure is not normalized. But if you want to go this path use:

select sub.student
FROM (
  select t.timestamp,
    t.Name,
    t.Total,
    c.col AS student,
    case c.col
      when 'Student1' then Student1
      when 'Student2' then Student2
      when 'Student3' then Student3
      -- ...
    end as d
  from mytable t
  cross join
  (
    select 'Student1' as col
    union all select 'Student2'
    union all select 'Student3'
    -- ...
  ) c
) AS sub
WHERE sub.timestamp = '20150911'
  AND sub.d > 0;
  -- sub.d = 'NA'
  -- sub.d = 0

SqlFiddleDemo

Output:

╔══════════╗
║ student  ║
╠══════════╣
║ Student1 ║
║ Student2 ║
╚══════════╝

If you want comma separated result use:

select GROUP_CONCAT(sub.student ORDER BY sub.student) AS result

SqlFiddleDemo2

Output:

╔═══════════════════╗
║       result      ║
╠═══════════════════╣
║ Student1,Student2 ║
╚═══════════════════╝

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 all columns greater than some value

From Dev

Excel: Select the cell to the left that is greater than/less than some value

From Java

Replace all elements of Python NumPy Array that are greater than some value

From Dev

Select columns thats greater than?

From Dev

'Greater than' some value in height()

From Dev

Is it possible to select all elements with an attribute value greater than a certain number?

From Dev

Is it possible to select all elements with an attribute value greater than a certain number?

From Dev

subset data.table keeping only elements greater than certain value applied to all columns

From Dev

Select where no value is greater than X

From Dev

Microsoft Excel how to select all cells that meet certain condition (i.e greater than a value)

From Dev

sql select where (sum of 2 columns) is greater than X

From Dev

Select Where Count() of multiple columns is greater than one

From Dev

Lanczos scale not working when scaleKey greater than some value

From Dev

php - Split an associative array with value greater than some integer

From Dev

Lanczos scale not working when scaleKey greater than some value

From Dev

How to check version is greater than some base value?

From Dev

Find first value in dataframe's columns greater than another

From Dev

SQLite select all records for which count is greater than 1

From Dev

SQL select all records only if the sum is greater than 0

From Dev

Jquery html "select" hide options with value greater than specified

From Dev

SQL - Select rows with values greater than max value for id and category

From Dev

How to select where sum of fields is greater than a value in MongoDB

From Dev

SQL query - select max where a count greater than value

From Dev

Compare rows in db and select if value is greater than 15%

From Dev

Select greater value than 0 if it exists, leave 0 if it is the only option

From Dev

MySQL: Select rows with values nearest but greater than a given value

From Dev

Select records having a time interval greater than a certain value in Teradata

From Dev

Select All Columns Except Some in Google BigQuery?

From Dev

Java Hashmap: get all keys greater than X value

Related Related

  1. 1

    Select all columns greater than some value

  2. 2

    Excel: Select the cell to the left that is greater than/less than some value

  3. 3

    Replace all elements of Python NumPy Array that are greater than some value

  4. 4

    Select columns thats greater than?

  5. 5

    'Greater than' some value in height()

  6. 6

    Is it possible to select all elements with an attribute value greater than a certain number?

  7. 7

    Is it possible to select all elements with an attribute value greater than a certain number?

  8. 8

    subset data.table keeping only elements greater than certain value applied to all columns

  9. 9

    Select where no value is greater than X

  10. 10

    Microsoft Excel how to select all cells that meet certain condition (i.e greater than a value)

  11. 11

    sql select where (sum of 2 columns) is greater than X

  12. 12

    Select Where Count() of multiple columns is greater than one

  13. 13

    Lanczos scale not working when scaleKey greater than some value

  14. 14

    php - Split an associative array with value greater than some integer

  15. 15

    Lanczos scale not working when scaleKey greater than some value

  16. 16

    How to check version is greater than some base value?

  17. 17

    Find first value in dataframe's columns greater than another

  18. 18

    SQLite select all records for which count is greater than 1

  19. 19

    SQL select all records only if the sum is greater than 0

  20. 20

    Jquery html "select" hide options with value greater than specified

  21. 21

    SQL - Select rows with values greater than max value for id and category

  22. 22

    How to select where sum of fields is greater than a value in MongoDB

  23. 23

    SQL query - select max where a count greater than value

  24. 24

    Compare rows in db and select if value is greater than 15%

  25. 25

    Select greater value than 0 if it exists, leave 0 if it is the only option

  26. 26

    MySQL: Select rows with values nearest but greater than a given value

  27. 27

    Select records having a time interval greater than a certain value in Teradata

  28. 28

    Select All Columns Except Some in Google BigQuery?

  29. 29

    Java Hashmap: get all keys greater than X value

HotTag

Archive