SELECT A.HOUR,A.ACTUAL_COUNT,B.EXPECTED_COUNT
FROM (
SELECT EXTRACT(HOUR FROM REPORT_INQUIRY_DT) HOUR ,COUNT(1) ACTUAL_COUNT
FROM SBE_RPT_INQ_OUTPUT
WHERE TO_CHAR(REPORT_INQUIRY_DT,'YYYY-MM-DD')='2015-04-19'
GROUP BY EXTRACT(HOUR FROM REPORT_INQUIRY_DT)
ORDER BY EXTRACT(HOUR FROM REPORT_INQUIRY_DT)
) A
left outer JOIN (
SELECT EXTRACT(HOUR FROM REPORT_INQUIRY_DT) AS HOUR,
ROUND(COUNT(1)/4) EXPECTED_COUNT
FROM (
SELECT *
FROM SBE_RPT_INQ_OUTPUT
WHERE TO_CHAR(REPORT_INQUIRY_DT,'YYYY-MM-DD')=TO_CHAR((SELECT SYSDATE -7 FROM DUAL),'YYYY-MM-DD')
UNION ALL
SELECT *
FROM SBE_RPT_INQ_OUTPUT
WHERE TO_CHAR(REPORT_INQUIRY_DT,'YYYY-MM-DD')=TO_CHAR((SELECT SYSDATE -14 FROM DUAL),'YYYY-MM-DD')
UNION ALL
SELECT *
FROM SBE_RPT_INQ_OUTPUT
WHERE TO_CHAR(REPORT_INQUIRY_DT,'YYYY-MM-DD')=TO_CHAR((SELECT SYSDATE -21 FROM DUAL),'YYYY-MM-DD')
UNION ALL
SELECT *
FROM SBE_RPT_INQ_OUTPUT
WHERE TO_CHAR(REPORT_INQUIRY_DT,'YYYY-MM-DD')=TO_CHAR((SELECT SYSDATE -34 FROM DUAL),'YYYY-MM-DD')
) A
GROUP BY EXTRACT(HOUR FROM A.REPORT_INQUIRY_DT)
ORDER BY EXTRACT(HOUR FROM A.REPORT_INQUIRY_DT)
) B
ON A.HOUR=B.HOUR;
和我得到的答案。
hour Actual_Count Expect_count
7 2 0
我想显示所有小时交易,即使它不是null
。
答案 0 :(得分:0)
我在您的查询中看到了一些奇怪的内容,但是,如果没有任何要求说明,我只能假设它正是您想要提取的内容,除非您没有获取任何行缺少值的小时数。
因此,要获取缺失值,您可以尝试此查询:
with hours as (
select rownum-1 as hour
from dual
connect by rownum <= 24
)
select h.HOUR, count(*)
from hours h
left join (
select *
from SBE_RPT_INQ_OUTPUT t
where t.REPORT_INQUIRY_DT >= (trunc(sysdate)-(34))
and t.REPORT_INQUIRY_DT < trunc(sysdate)
) t
on (
mod(trunc(t.REPORT_INQUIRY_DT)-trunc(sysdate)) in (7,14,21,34)
and hour = EXTRACT(HOUR FROM REPORT_INQUIRY_DT)
)
group by h.HOUR
无论如何,你很难理解为什么你不想在0到6,8到13,15到20和22到33之间匹配前几天的事件。
<强>更新强>
要获取当天的小时数和过去4周的平均小时数,您可以使用此其他查询
with hours as (
select rn, mod(EXTRACT(HOUR FROM systimestamp) + 24 - rn,24) as hour
from (
select rownum-1 as rn
from dual
connect by rownum <= 24
)
)
select sysdate, HOUR as hour, nvl(avg,0) as avg, nvl(current_count,0) as current_count
from hours h
left join (
select EXTRACT(HOUR FROM REPORT_INQUIRY_DT) as hour,
count(case when t.REPORT_INQUIRY_DT < trunc(sysdate) then 1 end)/(4*7) as avg,
count(case when t.REPORT_INQUIRY_DT >= trunc(sysdate) then 1 end) as current_count
from SBE_RPT_INQ_OUTPUT t
where t.REPORT_INQUIRY_DT >= (trunc(sysdate)-(4*7))
group by EXTRACT(HOUR FROM REPORT_INQUIRY_DT)
) t using (hour)
order by rn