count multiple columns in one query

前端 未结 3 1407
既然无缘
既然无缘 2021-01-27 06:33

Guys i have four queries:

Query #1:

select 
satisfaction_score,count(satisfaction_score) as Satisfaction_count
from j_survey_response
where satisfaction_         


        
相关标签:
3条回答
  • 2021-01-27 07:03

    Move them in subqueries and join them by score columns. Like this

    select 
       q1.satisfaction_score as score,
       q1.satisfaction_count,
       q2.response_count,
       q3.responder_count ,
       q4.service_count,
    from (query 1) q1
         join (query 2) q2 on q1.satisfaction_score=q2.response_score
         join (query 3) q3 on q1.satisfaction_score=q3.responder_score
         join (query 4) q4 on q1.satisfaction_score=q3.service_score
    
    0 讨论(0)
  • 2021-01-27 07:08

    You can UNION ALL the separate queries and apply conditional aggregation on the resulting set:

    select score,
           max(case when type = 'satisfaction' then count end) as satisfaction_count,
           max(case when type = 'response' then count end) as response_count,
           max(case when type = 'responder' then count end) as responder_count,
           max(case when type = 'service' then count end) as service_count             
    from (
      select satisfaction_score as score, 
             count(satisfaction_score) as count, 
             'satisfaction' as type
      from j_survey_response
      where satisfaction_score != 0
      group by satisfaction_score
    
      union all
    
      select response_score,
             count(response_score) as count, 'response' as type
      from j_survey_response
      where response_score != 0
      group by response_score
    
      union all
    
      select responder_score,
             count(responder_score) as count, 'responder' as type
      from j_survey_response
      where responder_score != 0
      group by responder_score
    
      union all
    
      select service_score,
             count(service_score) as count, 'service' as type
      from j_survey_response
      where service_score != 0
      group by service_score) as t
    group by score
    
    0 讨论(0)
  • 2021-01-27 07:24

    You Try this Sql Query

    select service_score , count(satisfaction_score) as Satisfaction_count,
    count(response_score) as response_count,count(responder_score) as `responder_count,
    count(service_score) as service_count from j_survey_response
    where service_score != 0 and responder_score != 0 and response_score != 0 and satisfaction_score != 0`
    

    if you use group by

    select service_score , count(satisfaction_score) as Satisfaction_count,
    count(response_score) as response_count,count(responder_score) as `responder_count,
    count(service_score) as service_count from j_survey_response
    where service_score != 0 and responder_score != 0 and response_score != 0 and satisfaction_score != 0` 
    group by satisfaction_score,response_score,responder_score,service_score
    
    0 讨论(0)
提交回复
热议问题