mysql - SQL - multiple counts for number matching rows in a table -


i have table looks this:

+-----------------------------+ | id | item1 | item2 | item3  | |  1 |    2  |   5   |   4    | |  2 |    5  |   5   |   2    | |  3 |    3  |   4   |   5    | +-----------------------------+ 

id unique integer , values 3 item columns integers range 1-5 (and may null). i'd sql statement returns, each item column, number of rows have value greater or equal 4. so, desired output of query be:

+-----------------------+ | item1 | item2 | item3 | |    1  |   3   |   2   | +-----------------------+ 

is there single query achieve this?

you use sum on case when

 select     sum( case when item1 > 4 1 else 0 end) item1   ,sum( case when item2 > 4 1 else 0 end) item2   ,sum( case when item3 > 4 1 else 0 end) item3 my_table  

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -