multiple select in one sql statement

后端 未结 2 687
Happy的楠姐
Happy的楠姐 2021-01-15 20:46

I have a table of survey answers, something like:

date     |  q1  | q2 |
12/12/10 | yes | no | 
12/13/10 | no  | no | 

and I would like to

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-15 21:11

    You could use:

    select q1, q2, count(*)
    from survey
    group by q1, q2
    

    Or, if you want to get those exact same results:

    select count(case when q1 = 'Yes' then q1 else null end) as q1_yes,
           count(case when q1 = 'No' then q1 else null end) as q1_no,
           count(case when q2 = 'Yes' then q2 else null end) as q2_yes
    from survey
    

    Your implementation of "case" may vary, the important thing is you can set everything you don't want to null and it won't be counted by count() :)

提交回复
热议问题