2017-04-06 71 views
0

我必須從Oracle表中獲取數據,其中我有一個名爲periodstarttime的日期字段,並且我只想獲取彙總的數據(聚合必須完成1小時數據),在幾分鐘00þ,15日,30日,45在間隔5分鐘oracle-從開始時間開始的1小時數據聚合15分鐘的時間

例如與輸入數據的一天,如果我的表我有periodstarttime作爲

periodstarttime  data 
05/04/2017 1:00:00  10 
05/04/2017 1:05:00  1 
05/04/2017 1:10:00  2 
05/04/2017 1:15:00  3 
05/04/2017 1:20:00  4 
05/04/2017 1:25:00  5 
05/04/2017 1:30:00 
and so on.... 

那麼我想我的結果看如:

periodstarttime   data with 1hr aggregation 
05/04/2017 1:00:00  data with 1hr aggregation from 1.00 to 2.00 
05/04/2017 1:15:00  data with 1hr aggregation from 1.15 to 2.15 
05/04/2017 1:30:00  data with 1hr aggregation from 1.30 to 2.30 
05/04/2017 1:45:00  data with 1hr aggregation from 1.45 to 2.45 
+1

創建實例以便你也可以給予預期的輸出。 – Utsav

回答

0

您可以使用TRUNC()CASE截斷到最近每隔15分鐘,然後用解析函數與一窗口子句在滾動窗口得到的總和:

SELECT DISTINCT 
     periodstarttime, 
     SUM(data) OVER (ORDER BY periodstarttime 
          RANGE BETWEEN INTERVAL '0' MINUTE PRECEDING 
           AND  INTERVAL '45' MINUTE FOLLOWING 
         ) 
     AS hour_total, 
     'data with 1hr aggregation from ' 
     || TO_CHAR(periodstarttime, 'HH24:MI') 
     || ' to ' 
     || TO_CHAR(periodstarttime + INTERVAL '1' HOUR, 'HH24:MI') 
     AS range 
FROM (
    SELECT CASE 
      WHEN periodstarttime < TRUNC(periodstarttime, 'HH24') + INTERVAL '15' MINUTE 
      THEN TRUNC(periodstarttime, 'HH24') 
      WHEN periodstarttime < TRUNC(periodstarttime, 'HH24') + INTERVAL '30' MINUTE 
      THEN TRUNC(periodstarttime, 'HH24') + INTERVAL '15' MINUTE 
      WHEN periodstarttime < TRUNC(periodstarttime, 'HH24') + INTERVAL '45' MINUTE 
      THEN TRUNC(periodstarttime, 'HH24') + INTERVAL '30' MINUTE 
      ELSE TRUNC(periodstarttime, 'HH24') + INTERVAL '45' MINUTE 
     END AS periodstarttime, 
     data 
    FROM table_name 
) 
ORDER BY periodstarttime; 
+0

,但是當periodstarttime落在00和15之間時,生成的表格包含相同的periodstarttime兩次或更多(00,0,0,10),但是我需要精確地聚合00,15,30,45,其中還包括05,10, 20,25 ...例如在一天中,我只需要96個樣本(periodstarttime爲00,15,30,45) – aaru

0

查看分析函數的Windowing_clause

會是這樣

select periodstarttime, 
    SUM(DATA) OVER (RANGE BETWEEN CURRENT ROW AND INTERVAL '1' HOUR FOLLOWING ORDER BY periodstarttime), 
    'data with 1hr aggregation from '||FIRST_VALUE(periodstarttime) OVER (RANGE BETWEEN CURRENT ROW AND INTERVAL '1' HOUR FOLLOWING ORDER BY periodstarttime) 
    ||' to '||LAST_VALUE(periodstarttime) OVER (RANGE BETWEEN CURRENT ROW AND INTERVAL '1' HOUR FOLLOWING ORDER BY periodstarttime) 
FROM ... 
WHERE EXTRACT(MINUTE FROM periodstarttime) IN (0,15,30,45) 
+0

這有很多問題:15分鐘內沒有聚合(即,如果'11:00'條目丟失,那麼'11:05'和'11:10'也將被忽略)。分析函數需要一個'ORDER BY'子句或者你得到'ORA-30485';如果'periodstarttime'是一個日期列,那麼使用'MINUTE'作爲'EXTRACT'的字段是無效的(它需要是一個'TIMESTAMP')。 – MT0

0

這裏是做這件事的一種方法:

WITH your_table AS (SELECT to_date('05/04/2017 01:00:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 10 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:05:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 20 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:10:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 30 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:15:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 40 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:20:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 50 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:25:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 60 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:30:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 70 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:40:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 80 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 01:50:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 90 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 02:00:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 100 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 02:10:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 110 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 02:20:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 120 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 02:30:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 130 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 02:40:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 140 DATA FROM dual UNION ALL 
        SELECT to_date('05/04/2017 03:00:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 150 DATA FROM dual) 
-- End of setting up data in your_table; you would not need the above since you already have a table with data in it 
-- See the below SQL for the main statement: 
SELECT DISTINCT TRUNC(periodstarttime, 'hh') + FLOOR((TRUNC(periodstarttime, 'mi') - TRUNC(periodstarttime, 'hh'))/(15/1440))*15/1440 periodstarttime, 
     sum(DATA) OVER (ORDER BY TRUNC(periodstarttime, 'hh') + FLOOR((TRUNC(periodstarttime, 'mi') - TRUNC(periodstarttime, 'hh'))/(15/1440))*15/1440 
         RANGE BETWEEN 0 PRECEDING AND 60/1440 FOLLOWING) sum_over_next_hour 
FROM your_table 
ORDER BY periodstarttime; 

PERIODSTARTTIME SUM_OVER_NEXT_HOUR 
---------------- ------------------ 
05/04/2017 01:00    660 
05/04/2017 01:15    850 
05/04/2017 01:30    770 
05/04/2017 01:45    690 
05/04/2017 02:00    750 
05/04/2017 02:15    540 
05/04/2017 02:30    290 
05/04/2017 03:00    150 

這是通過:

  1. 尋找週期的開始時間,這是我們做的通過查找每個週期開始時間的分鐘數,將其除以15分鐘,找到該數字的底部,然後將其乘以新的號碼15分鐘。這將時間分成15個分鐘組。

  2. 現在我們知道這些羣體,我們可以做一個累計和。因爲我們希望整個小時的價值,我們需要在當前時期和當前時期+ 1小時之間做一個範圍。注:這是包容性的,因此02:00的值將包含在01:00期間的累積金額中。如果你不想這樣做,請改變範圍,使其達到59分鐘(這很好,因爲我們的時間段現在是過去一小時的0,15,30或45分鐘)。

相關問題