在Oracle中获取查询结果15秒间隔

时间:2018-11-26 19:53:38

标签: sql oracle

我有以下查询以获取15秒间隔。但是,即使结果没有值,我也想显示“ 0”值。

这是查询;

select to_char(trunc(sample_time,'hh24') + (trunc(to_char(sample_time,'mi')/1)*1)/1440 + (trunc(to_char(sample_time,'ss')/15)*15)/86400,'hh24:mi:ss') as SAMPLE_TIME,nvl(wait_class,'CPU') as waits,round(count(*)/15,2)
from gv$active_session_history
where sample_time >= sysdate-60/1440
group by wait_class,to_char(trunc(sample_time,'hh24') + (trunc(to_char(sample_time,'mi')/1)*1)/1440  + (trunc(to_char(sample_time,'ss')/15)*15)/86400,'hh24:mi:ss')
order by 1 desc,3 desc;

结果是;

SAMPLE_TIME    WAITS   COUNT
-----------------------------
14:59:30        CPU     3
14:59:00        CPU     2    
14:58:45        CPU     2
14:58:30        CPU     1
14:58:15        CPU     2
14:57:45        CPU     2

这是我想要的;

    SAMPLE_TIME    WAITS   COUNT
    -----------------------------
    14:59:30        CPU     3
    14:59:15        CPU     0  // Added 14:59:15    
    14:59:00        CPU     2    
    14:58:45        CPU     2
    14:58:30        CPU     1
    14:58:15        CPU     2
    14:58:00        CPU     0  // Added 14:59:15
    14:57:45        CPU     2

我应该从上述查询中更改什么?

1 个答案:

答案 0 :(得分:2)

您可以按以下方式使用子查询来生成最近一小时内间隔15秒的所有日期的列表:

select trunc(sysdate, 'mi') - 15*level/(60*60*24) 
from dual
connect by level < 60*60/15;

在查询中插入该内容:

with a as (
    select trunc(sysdate, 'mi') - 15*level/(60*60*24) as sample_time 
    from dual 
    connect by level < 60*60/15
)
select 
    a.sample_time,
    nvl(wait_class,'cpu') as waits,
    round(sum(decode(ash.wait_class, null, 0, 1))/15,2)
from a
left join gv$active_session_history ash 
    on ash.sample_time between a.sample_time and a.sample_time + 15/(60*60*24)
group by a.sample_time, wait_class
order by 1 desc,3 desc;