2012-12-11 103 views
15

我想通過時間戳執行移動平均值。 我有兩列:溫度和時間戳(時間日期),我想基於每15分鐘連續的溫度觀測執行移動平均值。換句話說,選擇數據來執行基於15分鐘時間間隔的平均值。而且,對於不同的時間序列可能具有不同數量的觀察值。我的意思是所有的窗口大小都是相等的(15分鐘),但每個窗口中可能有不同數量的觀察值。 例如: 對於第一個窗口,我們必須計算n個觀測值的平均值,第二個窗口計算n + 5個觀測值的平均值。基於PostgreSQL中時間戳的移動平均值

數據樣本:

 
ID Timestamps   Temperature 
1 2007-09-14 22:56:12 5.39 
2 2007-09-14 22:58:12 5.34 
3 2007-09-14 23:00:12 5.16 
4 2007-09-14 23:02:12 5.54 
5 2007-09-14 23:04:12 5.30 
6 2007-09-14 23:06:12 5.20 
7 2007-09-14 23:10:12 5.39 
8 2007-09-14 23:12:12 5.34 
9 2007-09-14 23:20:12 5.16 
10 2007-09-14 23:24:12 5.54 
11 2007-09-14 23:30:12 5.30 
12 2007-09-14 23:33:12 5.20 
13 2007-09-14 23:40:12 5.39 
14 2007-09-14 23:42:12 5.34 
15 2007-09-14 23:44:12 5.16 
16 2007-09-14 23:50:12 5.54 
17 2007-09-14 23:52:12 5.30 
18 2007-09-14 23:57:12 5.20 

主要挑戰:

如何學習的代碼來區分每15分鐘雖然有可能不準確,因爲不同的採樣頻率時間15分鐘一班。

+0

如果新的15分鐘窗口啓動,滾動平均值是否會「重新啓動」?還是應該平均計算「最後」15分鐘? –

+0

@a_horse_with_no_name,實際上,數據集包含4周的歷史數據,我需要移動平均結果作爲新的數據集。 –

+0

這不能回答我的問題。 –

回答

5

假設要每次15分鐘間隔後,重新啓動滾動平均值:

select id, 
     temp, 
     avg(temp) over (partition by group_nr order by time_read) as rolling_avg 
from (  
    select id, 
     temp, 
     time_read, 
     interval_group, 
     id - row_number() over (partition by interval_group order by time_read) as group_nr 
    from (
    select id, 
      time_read, 
      'epoch'::timestamp + '900 seconds'::interval * (extract(epoch from time_read)::int4/900) as interval_group, 
      temp 
    from readings 
) t1 
) t2 
order by time_read; 

它是由「時間範圍」的基礎上Depesz's solution到組:

下面是一個SQLFiddle示例:http://sqlfiddle.com/#!1/0f3f0/2

9

你可以用自己加入你的表:

select l1.id, avg(l2.Temperature) 
from l l1 
inner join l l2 
    on l2.id <= l1.id and 
     l2.Timestamps + interval '15 minutes' > l1.Timestamps 
group by l1.id 
order by id 
; 

Results

| ID |   AVG | 
----------------------- 
| 1 |   5.39 | 
| 2 |   5.365 | 
| 3 | 5.296666666667 | 
| 4 |   5.3575 | 
| 5 |   5.346 | 
| 6 | 5.321666666667 | 
| 7 | 5.331428571429 | 

注意:只有 '拼搏' 而成。您應該將結果與原始表結合或追加新列以進行查詢。我不知道你需要的最終查詢。調整此解決方案或尋求更多幫助。

3

下面是一個利用設施將聚合函數用作窗口函數的方法。聚合函數會保留數組中最後15分鐘的觀察值以及當前的運行總數。狀態轉換功能將元素從陣列中移出,落在15分鐘的窗口後面,並推動最近的觀察。最終的功能只是計算陣列中的平均溫度。

現在,至於這是否是一種好處......取決於。它側重於postgresql的plgpsql執行部分,而不是數據庫訪問部分,而我自己的經驗是plpgsql不快。如果您可以輕鬆查找表格以查找每個觀察結果的前15分鐘行,則自行加入(如在@danihp答案中)將會很好。然而,這種方法可以處理來自更復雜的來源的觀察結果,其中這些查找是不實際的。像以往一樣,試用並在自己的系統上進行比較。

-- based on using this table definition 
create table observation(id int primary key, timestamps timestamp not null unique, 
         temperature numeric(5,2) not null); 

-- note that I'm reusing the table structure as a type for the state here 
create type rollavg_state as (memory observation[], total numeric(5,2)); 

create function rollavg_func(state rollavg_state, next_in observation) returns rollavg_state immutable language plpgsql as $$ 
declare 
    cutoff timestamp; 
    i int; 
    updated_memory observation[]; 
begin 
    raise debug 'rollavg_func: state=%, next_in=%', state, next_in; 
    cutoff := next_in.timestamps - '15 minutes'::interval; 
    i := array_lower(state.memory, 1); 
    raise debug 'cutoff is %', cutoff; 
    while i <= array_upper(state.memory, 1) and state.memory[i].timestamps < cutoff loop 
    raise debug 'shifting %', state.memory[i].timestamps; 
    i := i + 1; 
    state.total := state.total - state.memory[i].temperature; 
    end loop; 
    state.memory := array_append(state.memory[i:array_upper(state.memory, 1)], next_in); 
    state.total := coalesce(state.total, 0) + next_in.temperature; 
    return state; 
end 
$$; 

create function rollavg_output(state rollavg_state) returns float8 immutable language plpgsql as $$ 
begin 
    raise debug 'rollavg_output: state=% len=%', state, array_length(state.memory, 1); 
    if array_length(state.memory, 1) > 0 then 
    return state.total/array_length(state.memory, 1); 
    else 
    return null; 
    end if; 
end 
$$; 

create aggregate rollavg(observation) (sfunc = rollavg_func, finalfunc = rollavg_output, stype = rollavg_state); 

-- referring to just a table name means a tuple value of the row as a whole, whose type is the table type 
-- the aggregate relies on inputs arriving in ascending timestamp order 
select rollavg(observation) over (order by timestamps) from observation;