Oracle事件计数查询

Oracle事件计数查询,第1张

Oracle事件计数查询

尝试:

CREATE OR REPLACe VIEW my_view ASWITH date_bookends AS (  SELECT LEAST(MIN(t.sampled_on), MIN(t.received_on), MIN(t.completed_on), MIN(t.authorized_on)) 'min_date'         GREATEST(MAX(t.sampled_on), MAX(t.received_on), MAX(t.completed_on), MAX(t.authorized_on)) 'max_date'    FROM SAMPLE t),    all_hours AS (  SELECt t.min_date + numtodsinterval(LEVEL - 1,'hour') date_by_hour    FROM date_bookends tConNECT BY LEVEL <= ( t.max_date - t.min_date + 1) * 24)SELECt h.date_by_hour,       COUNT(CASE WHEN h.hour = TRUNC(s.sampled_on,'hh24') THEN 1 END) sampled#       COUNT(CASE WHEN h.hour = TRUNC(s.received_on,'hh24') THEN 1 END) received#       COUNT(CASE WHEN h.hour = TRUNC(s.completed_on,'hh24') THEN 1 END) completed#       COUNT(CASE WHEN h.hour = TRUNC(s.authorized_on,'hh24') THEN 1 END) authorized#  FROM all_hours hCROSS JOIN sample s  GROUP BY h.hour

不使用子查询分解

CREATE OR REPLACe VIEW my_view ASSELECT h.date_by_hour,       COUNT(CASE WHEN h.hour = TRUNC(s.sampled_on,'hh24') THEN 1 END) sampled#       COUNT(CASE WHEN h.hour = TRUNC(s.received_on,'hh24') THEN 1 END) received#       COUNT(CASE WHEN h.hour = TRUNC(s.completed_on,'hh24') THEN 1 END) completed#       COUNT(CASE WHEN h.hour = TRUNC(s.authorized_on,'hh24') THEN 1 END) authorized#  FROM (SELECt t.min_date + numtodsinterval(LEVEL - 1,'hour') date_by_hour         FROM (SELECt LEAST(MIN(t.sampled_on), MIN(t.received_on), MIN(t.completed_on), MIN(t.authorized_on)) 'min_date'      GREATEST(MAX(t.sampled_on), MAX(t.received_on), MAX(t.completed_on), MAX(t.authorized_on)) 'max_date' FROM SAMPLE t) tConNECT BY LEVEL <= ( t.max_date - t.min_date + 1) * 24) hCROSS JOIN sample sGROUP BY h.hour

该查询两次访问SAMPLES表-第一次获取最早和最新的日期来构造

date_by_hour
值的构造。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5640433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存