在Oracle上进行数据透视查询

时间:2017-06-24 21:10:30

标签: sql oracle

我想在不使用'Pivot'功能的情况下制作一个数据透视表,因为10g不支持它。

我尝试了很多方法但是我失败了很多。你能帮我把下面的查询转换成我在下面指定的数据透视表吗?

select sysdate as SAMPLE_TIME,inst_id,wait_class,SUM(waits) as waits from (SELECT gv$system_event.inst_id,gv$system_event.wait_class, gv$system_event.time_waited as waits
        FROM gv$system_event where wait_class != 'Idle' union select inst_id, 'CPU' as wait_class, round(value/10000) as waits from gv$sys_time_model where stat_name = 'DB CPU') results
        where waits!=0
        group by wait_class,inst_id;

结果如下;

  SAMPLE_TIME | INST_ID | WAIT_CLASS  | WAITS
---------------------------------------------
25/06/2017    2         User I/O    149719629
25/06/2017    1         User I/O    33314833
25/06/2017    2         System I/O  130276500
25/06/2017    1         System I/O  47508145

我想要的是这个;

SAMPLE_TIME | INST_ID | User I/O | System I/O
---------------------------------------------
25/06/2017      1       33314833    47508145
26/06/2017      2       149719629   130276500

1 个答案:

答案 0 :(得分:1)

将SUM与案例表达式一起使用:

SELECT sysdate as SAMPLE_TIME,inst_id,
       SUM( CASE WHEN WAIT_CLASS = 'User I/O'
            THEN waits END ) As "User I/O",
       SUM( CASE WHEN WAIT_CLASS = 'System I/O'
            THEN waits END ) As "System I/O" 
FROM ( .....
.....
.....
)
GROUP BY sysdate, inst_id