更改SELECT结果的布局

时间:2016-11-16 16:46:42

标签: mysql

我有以下查询,我想更改结果的显示方式。我研究过使用枢轴,但是我无法理解我是如何使它工作的,并且非常感激你的帮助。

(缩短)查询:

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

我有什么建议可以实现这个目标吗?

1 个答案:

答案 0 :(得分: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')