Oracle Grouping持续24小时间隔2小时没有间隙

时间:2015-09-29 13:37:17

标签: sql oracle datetime group-by

我想在过去24小时内每隔2小时写一个Amounts的查询。没有金额的小时数应该返回零。

input_date | amount ------------------------------ 29/07/2015-11:03:00 | 100 29/07/2015-10:45:02 | 100 29/07/2015-08:22:57 | 100 29/07/2015-06:50:45 | 12000 28/07/2015-22:16:48 | 235 表的内容:

SYSDATE

29/07/2015-11:05 date | sum ----------------------------- 29/07/2015-10:00:00 | 200 29/07/2015-08:00:00 | 100 29/07/2015-06:00:00 | 12000 29/07/2015-04:00:00 | 0 29/07/2015-02:00:00 | 0 29/07/2015-00:00:00 | 0 28/07/2015-22:00:00 | 235 29/07/2015-20:20:00 | 0 29/07/2015-18:00:00 | 0 29/07/2015-16:00:00 | 0 29/07/2015-14:00:00 | 0 29/07/2015-12:00:00 | 0 时,预期结果应为

{{1}}

2 个答案:

答案 0 :(得分:2)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE data (
 input_date,
 amount
) AS
          SELECT TO_DATE( '29/09/2015-11:03:00', 'DD/MM/YYYY-HH24:MI:SS' ), 100 FROM DUAL
UNION ALL SELECT TO_DATE( '29/09/2015-10:45:02', 'DD/MM/YYYY-HH24:MI:SS' ), 100 FROM DUAL
UNION ALL SELECT TO_DATE( '29/09/2015-08:22:57', 'DD/MM/YYYY-HH24:MI:SS' ), 100 FROM DUAL
UNION ALL SELECT TO_DATE( '29/09/2015-06:50:45', 'DD/MM/YYYY-HH24:MI:SS' ), 12000 FROM DUAL
UNION ALL SELECT TO_DATE( '28/09/2015-22:16:48', 'DD/MM/YYYY-HH24:MI:SS' ), 235 FROM DUAL;

查询1

WITH two_hour_windows ( time ) AS (
  SELECT     TRUNC( SYSDATE, 'HH24' ) - ( LEVEL * 2 - 2 + MOD( TO_CHAR( SYSDATE, 'HH24' ), 2 ) )/24
  FROM       DUAL
  CONNECT BY LEVEL <= 12
)
SELECT time,
       COALESCE( SUM( amount ), 0 ) AS total_amount
FROM   two_hour_windows t
       LEFT OUTER JOIN data d
       ON ( time <= input_date AND input_date < time + 1/12 )
GROUP BY time
ORDER BY time

<强> Results

|                        TIME | TOTAL_AMOUNT |
|-----------------------------|--------------|
| September, 28 2015 16:00:00 |            0 |
| September, 28 2015 18:00:00 |            0 |
| September, 28 2015 20:00:00 |            0 |
| September, 28 2015 22:00:00 |          235 |
| September, 29 2015 00:00:00 |            0 |
| September, 29 2015 02:00:00 |            0 |
| September, 29 2015 04:00:00 |            0 |
| September, 29 2015 06:00:00 |        12000 |
| September, 29 2015 08:00:00 |          100 |
| September, 29 2015 10:00:00 |          200 |
| September, 29 2015 12:00:00 |            0 |
| September, 29 2015 14:00:00 |            0 |

答案 1 :(得分:0)

这是一个1小时的间隔。我不知道如何绕它2小时。

SELECT times1.input_date,NVL(docs.amount,0) amount   
FROM   
(SELECT TO_CHAR(TRUNC(input_date, 'HH'),'yyyymmddhh24') input_date, SUM(amount)   amount   
 FROM amounts  
 WHERE (input_date >= sysdate- TO_CHAR( 24 ) /24 )   
 GROUP BY TRUNC(input_date, 'HH')   
 ) docs  
 RIGHT OUTER JOIN   
 (select TO_CHAR(ROUND(sysdate, 'HH')- rownum/24,'yyyymmddhh24') input_date, 0 amount  
  from all_objects   
  where rownum <=  TO_CHAR( 24 )  
 ) times1  
 ON (docs.input_date = times1.input_date)  
 ORDER BY times1.input_date DESC;