2016-11-16 57 views
1

我有下面的查詢,我想改變結果的顯示方式。我看着使用樞軸,但我無法理解我如何使它工作,並希望得到幫助。更改SELECT結果的佈局

的(縮短)的查詢:

select 
# total counts 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 5,1, null)) as under_5_sec, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 10,1, null)) as under_10_sec, 

# percentage answered of calls within timeframe 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 5,1, null))/COUNT(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as under_5_perct, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 10,1, null))/COUNT(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as under_10_perct, 

# percentage answered of offered calls 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 5,1, null))/COUNT(if(verb in ('ENTERQUEUE') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as under_5_offer, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 10,1, null))/COUNT(if(verb in ('ENTERQUEUE') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as under_10_offer 

from queue_log 

where partition = 'P001' 
and time_id >= unix_timestamp('2016-10-30') and time_id < unix_timestamp('2016-10-31') 

的結果顯示爲:

under_5_sec | under_10_sec | under_5_perct | under_10_perct | under_5_offer | under_10_offer 
346   | 353   | 91.7772  | 93.6340   | 87.3737  | 89.1414 

我想以顯示數據:

descr  | per_sec | percent | offered 
under_5_sec | 346  | 91.7772 | 87.3737 
under_10_sec| 353  | 93.6340 | 89.1414 

任何建議如何我可以做到這一點?

+0

的可能的複製[MySQL的樞軸表(http://stackoverflow.com/questions/7674786/mysql-pivot-table) – CGritton

回答

1
select 
'under_5_sec' as descr, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 5,1, null)) as per_sec, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 5,1, null))/COUNT(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as perct, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 5,1, null))/COUNT(if(verb in ('ENTERQUEUE') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as offer, 
from queue_log 
where partition = 'P001' 
and time_id >= unix_timestamp('2016-10-30') and time_id < unix_timestamp('2016-10-31') 
UNION 
select 
'under_10_sec' as descr, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 10,1, null)) as per_sec, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 10,1, null))/COUNT(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as perct, 
count(if(verb in ('COMPLETEAGENT','COMPLETECALLER') and queue in (9100, 9102, 9104) and data1 <= 10,1, null))/COUNT(if(verb in ('ENTERQUEUE') and queue in (9100, 9102, 9104, 9106, 9108, 9600, 9602, 9500, 9502, 9800, 9802),1, null)) * 100 as offer 
from queue_log 
where partition = 'P001' 
and time_id >= unix_timestamp('2016-10-30') and time_id < unix_timestamp('2016-10-31') 
+0

謝謝,這確實給我所期望的結果。像這樣使用聯合運行查詢兩次(每個工會)? – Stephen

+0

你需要這種結果的聯合。我不知道MySQL的內部。但我認爲查詢每個記錄評估一次,而不是兩次。我的意思是表格記錄不會與一個接一個的查詢進行比較。 –

+0

聯盟創建兩個數據集,然後「聯合」形成一個查詢結果。 –