在一个查询中计算多个列

时间:2016-02-16 11:15:25

标签: mysql sql

伙计们我有四个问题:

查询#1:

select 
satisfaction_score,count(satisfaction_score) as Satisfaction_count
from j_survey_response
where satisfaction_score != 0
group by satisfaction_score

输出

satisfaction_score    Satisfaction_count
1                           5
2                           8
3                          97
4                         329
5                         859

查询#2:

select 
response_score,count(response_score) as response_count
from j_survey_response
where response_score != 0
group by response_score

输出

response_score  response_count
1                    28
2                     8
3                    42
4                   250
5                   980

查询#3:

select 
responder_score,count(responder_score) as responder_count
from j_survey_response
where responder_score != 0
group by responder_score

输出

responder_score  responder_count
1                     24
2                      3
3                     30
4                    236
5                    987

查询#4:

select 
service_score,count(service_score) as service_count
from j_survey_response
where service_score != 0
group by service_score

输出

service_score    service_count
1                     22
2                      2
3                     34
4                    270
5                    966

但我需要输出如下

score  satisfaction_count  response_count  responder_count  service_count
1            5                  28              24               22
2            8                   8               3                2
3           97                  42              30               34
4          329                 250             236              270
5          859                 980             986              966

3 个答案:

答案 0 :(得分:2)

您可以UNION ALL单独的查询并在结果集上应用条件聚合:

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

答案 1 :(得分:1)

在子查询中移动它们并按​​分数列连接它们。喜欢这个

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

答案 2 :(得分:0)

您尝试此SQL查询

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`

如果您使用分组

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