2016-02-16 84 views
0

專家多個列我有四個查詢:計數在一個查詢

查詢#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 

OUTPUT

 
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 

OUTPUT

 
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 
+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' 

? – jarlh

回答

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 
+0

如果1的值在satisfaction_score列中不存在,該怎麼辦?輸出將從第二列開始 –

+0

@JeevaBalan是的,這是正確的。如果該值不在*所有表格*中,則該值不會出現在最終結果中。在這種情況下,您需要一個數字表格來保存所有可能的分數值。使用這個表格,你可以將上面的查詢中的表格連接起來。 –

+0

我嘗試了上面的查詢,但是它顯示錯誤1604:在'''''''''''''''''''''''''''' –

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 
+0

如果在四個子查詢之一中有一個評分桶爲空,該怎麼辦? –

+0

@GiorgosBetsos發佈的輸出很好。通常情況下可能需要全部加入。但實際上它更好地定義一個具有所需分數值的單獨表格,並將所有子查詢與分數數據集連接起來 – StanislavL

+0

它顯示錯誤錯誤代碼:1248.每個派生表必須具有其自己的別名 –

0

你,如果你使用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