我想创建一个显示日期和计数的查询。我想显示一个范围内的所有日期,包括零计数的日期。我正在寻找一种不使用临时表的方法。
输出可能如下所示:
Time Count
1/1/2017 01:00 AM 23
1/1/2017 02:00 AM 0
1/1/2017 03:00 AM 0
1/1/2017 04:00 AM 12
答案 0 :(得分:1)
我能够提出一个添加零计数行的解决方案。结果集的示例如下。您将看到有一些重复的DayOfWeekByHour值。我需要弄清楚如何总结这些,但这基本上是我想要的。请参阅示例代码链接
DayOfWeekByHour,Count
100,0
101,0
102,0
100,23
201,100
SELECT
TO_NUMBER(MOD(V,7)+1 || LPAD(MOD(V,24),2,'0')) as DayOfWeekByHour, 0
FROM (
SELECT 1 V FROM DUAL
) T
MODEL DIMENSION BY (ROWNUM R)
MEASURES (V)
RULES ITERATE (168) (
V[ITERATION_NUMBER] = CV(R) + 1
)
UNION
SELECT
TO_NUMBER(
TO_CHAR(
TO_DATE(
EXTRACT(MONTH FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(DAY FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(YEAR FROM ve.CREATED_TIMESTAMP) ,'MM/DD/YYYY'),'D'),'99') * 100 + TO_NUMBER(TO_CHAR(TO_DATE(
EXTRACT(MONTH FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(DAY FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(YEAR FROM ve.CREATED_TIMESTAMP) || ' ' ||
EXTRACT(HOUR FROM ve.CREATED_TIMESTAMP) || ':' ||
EXTRACT(MINUTE FROM ve.CREATED_TIMESTAMP)
,'MM/DD/YYYY HH24:MI'
)
,'HH24'
)
, '99') AS DayOfWeekByHour,
COUNT(*)
FROM
VIEWER.VIEWING_EVENT ve
WHERE
ve.CREATED_TIMESTAMP >= TO_TIMESTAMP('02-12-2016 00:00:00', 'dd-mm-yyyy hh24:mi:ss') AND
ve.CREATED_TIMESTAMP <= TO_TIMESTAMP('02-12-2017 00:00:00', 'dd-mm-yyyy hh24:mi:ss')
GROUP BY
TO_NUMBER(
TO_CHAR(
TO_DATE(
EXTRACT(MONTH FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(DAY FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(YEAR FROM ve.CREATED_TIMESTAMP) ,'MM/DD/YYYY'),'D'),'99') * 100 + TO_NUMBER(TO_CHAR(TO_DATE(
EXTRACT(MONTH FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(DAY FROM ve.CREATED_TIMESTAMP) || '/' ||
EXTRACT(YEAR FROM ve.CREATED_TIMESTAMP) || ' ' ||
EXTRACT(HOUR FROM ve.CREATED_TIMESTAMP) || ':' ||
EXTRACT(MINUTE FROM ve.CREATED_TIMESTAMP)
,'MM/DD/YYYY HH24:MI'
)
,'HH24'
)
, '99')
ORDER BY
DayOfWeekByHour
;