I have the a table like this.
| id | question_id | option_id |
---------------------------------
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
| 1 | 1 | 2 |
| 1 | 1 | 2 |
| 1 | 1 | 6 |
How would I query for the result as follows?
[
{'1' => 1},
{'2' => 3},
{'3' => 1},
{'4' => 0},
{'5' => 0},
{'6' => 1},
]
Currently what I have is by looping the query from 1 to 6. Is there a way for me to query the counts for all possible value of option_id without knowing about the possible values (1 to 6)?
Select sum(case when option_id = 1 then 1 else 0 end) as "1" FROM fcs_answer WHERE question_id = 4;