SQL count specific value over multiple columns and rows

后端 未结 5 1172
甜味超标
甜味超标 2021-01-18 06:35

I feel as if this should be quite easy, but can\'t seem to find a solution. Suppose I have the following table:

|--------||---||---||---||---||---||---||---|         


        
5条回答
  •  情话喂你
    2021-01-18 07:31

    Use conditional COUNT

    SELECT COUNT(case when q1 = '1' then 1 end)  + 
           COUNT(case when q2 = '1' then 1 end)  + 
           COUNT(case when q3 = '1' then 1 end)  + 
           COUNT(case when q4 = '1' then 1 end)  + 
           COUNT(case when q5 = '1' then 1 end)  + 
           COUNT(case when q6 = '1' then 1 end)  + 
           COUNT(case when q7 = '1' then 1 end) as ones_total
    FROM table
    WHERE Company = 'abc'
    

提交回复
热议问题