2012-10-22 7 views
7

Ho un set di dati che contiene osservazioni per diverse settimane con una frequenza di 2 minuti. Desidero aumentare l'intervallo di tempo da 2 minuti a 5 minuti. Il problema è che la frequenza delle osservazioni non è sempre la stessa. Voglio dire, in teoria, ogni 10 minuti dovrebbero esserci 5 osservazioni ma di solito non è il caso. Per favore fatemi sapere come posso aggregare le osservazioni in base alla funzione media e rispetto all'ora e alla data delle osservazioni. In altre parole aggregazione basata ogni 5 minuti mentre il numero di osservazioni non è lo stesso per ciascun intervallo di tempo di 5 minuti. Inoltre, ho la data e l'ora in formato timestamp.Esecuzione dell'aggregazione tramite data e ora in SQL

Esempio Dati:

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 

risultati attesi:

1 2007-09-14 23:00 5.29 
2 2007-09-14 23:05 5.34 
+0

dati di esempio post: cosa hai, di cosa hai bisogno. Scrivilo come inserti per essere facile testare un campione. Inoltre, facci sapere che marca di database stai usando. – danihp

+0

@danihp Esempio di dati: [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] risultati previsti: 1 2007-09-14 23:00 5.29 2 2007-09-14 23:06 5.34, sto usando PostgreSQL –

+2

@aliamidi - Dovresti davvero mettere questo tipo di informazioni nella domanda, non un commento. Vedi la modifica che ho fatto alla domanda per te ... Inoltre, per favore puoi spiegare perché quell'output è quello che ti aspettavi? Perché il secondo disco "23: 06" e non "23: 05"? E da dove viene il previsto '5.34'? – MatBailie

risposta

6

Le risposte a this question probabilmente forniscono buone soluzioni al problema, mostrando modi per aggregare in modo efficiente i dati in finestre temporali.

In sostanza, utilizzare il avg aggregata con:

GROUP BY floor(extract(epoch from the_timestamp)/60/5) 
1

Di gran lunga l'opzione più semplice è quello di creare una tabella di riferimento. In quel tavolo di memorizzare gli intervalli su cui si insterested: (. Adattare questo per il proprio data la notazione di RDBMS)

CREATE TABLE interval (
    start_time DATETIME, 
    cease_time DATETIME 
); 
INSERT INTO interval SELECT '2012-10-22 12:00', '2012-10-22 12:05'; 
INSERT INTO interval SELECT '2012-10-22 12:05', '2012-10-22 12:10'; 
INSERT INTO interval SELECT '2012-10-22 12:10', '2012-10-22 12:15'; 
INSERT INTO interval SELECT '2012-10-22 12:15', '2012-10-22 12:20'; 
INSERT INTO interval SELECT '2012-10-22 12:20', '2012-10-22 12:25'; 
INSERT INTO interval SELECT '2012-10-22 12:25', '2012-10-22 12:30'; 
INSERT INTO interval SELECT '2012-10-22 12:30', '2012-10-22 12:35'; 
INSERT INTO interval SELECT '2012-10-22 12:35', '2012-10-22 12:40'; 

Poi basta join e aggregati ...

SELECT 
    interval.start_time, 
    AVG(observation.value) 
FROM 
    interval 
LEFT JOIN 
    observation 
    ON observation.timestamp >= interval.start_time 
    AND observation.timestamp < interval.cease_time 
GROUP BY 
    interval.start_time 

NOTA: È necessario creare e compilare una sola volta la tabella degli intervalli, quindi riutilizzarla più volte.

+1

Perché fare l'inserto così complicato usando 'insert ... select'? Una semplice clausola 'values' è molto più diretta. –

+0

Tendo ad essere d'accordo con @a_horse_with_no_name; che "inserisci ... seleziona" è piuttosto strano. Un 'VALUES ('first', 'row'), ('second', 'row');' lista è molto più chiara e semplice. Produrre manualmente i valori è strano quando puoi semplicemente usare 'generate_series' per aggiungere intervalli in pochi minuti ad una data base, comunque. –

2

EDIT: Ho fatto un po 'di più a pensare a questo e ho capito che non si può solo andare da 2-min per 5 min. Non si sommano. Seguirò il problema, ma il codice seguente funziona quando si raccolgono dati da 1 minuto!

-

Se i dati sono in un formato 'inizio' è possibile utilizzare il codice all'interno di questa funzione, o creare la funzione sul proprio database per la facilità di accesso:

CREATE OR REPLACE FUNCTION dev.beginning_datetime_floor(timestamp without time zone, 
integer) /* switch out 'dev' with your schema name */ 
RETURNS timestamp without time zone AS 
$BODY$ 
SELECT 
date_trunc('minute',timestamp with time zone 'epoch' + 
floor(extract(epoch from $1)/($2*60))*$2*60 
* interval '1 second') at time zone 'CST6CDT' /* change this to your time zone */ 
$BODY$ 
LANGUAGE sql VOLATILE; 

Basta alimentano il numero intero di minuti per aggregare on (uso 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, o 30), ecco un paio risultati:

select dev.beginning_datetime_floor('2012-01-01 02:02:21',2) 

= '2012-01-01 02:02:00'

select dev.beginning_datetime_floor('2012-01-01 02:02:21',5) 

= '2012-01-01 02:00:00'

Basta provarlo e aggiungere o sottrarre tempo per gestire iniziando contro terminando timestamp utilizzando il built-in timestamp functions.

Quando si ottiene il timestamp desiderato, fare ciò che Craig ha detto e GROUP BY su quel timestamp, in combinazione con il desiderato aggregate functions (medie probabili).

Si potrebbe provare/modificarlo con:

date_trunc('minute',timestamp with time zone 'epoch' + 
floor(extract(epoch from your_datetime)/(interval_minutes*60))*interval_minutes*60 
* interval '1 second') at time zone 'CST6CDT' /* change this to your time zone */ 

Può succedere che si desidera fare la media i timestamp - se la durata dell'intervallo è volatile, per esempio. Per questo, è possibile creare una funzione simile che arrotonda il timestamp invece di prendere un piano.

1

Ok, quindi questo è solo un modo per gestirlo. Spero che questo ti faccia riflettere su come convertire i dati per le tue esigenze di analisi.

C'è un prerequisito per testare questo codice. È necessario disporre di una tabella con tutti i timestamp di 1 minuto possibili. Ci sono molti modi per farlo, userò solo quello che ho a disposizione, che è una tabella: dim_time che ha ogni minuto (00:01:00) attraverso (23:59:00) e un'altra tabella con tutte le possibili date (dim_date). Quando ti unisci a questi (1 = 1) ottieni tutti i minuti possibili per tutti i giorni possibili.

--first you need to create some functions I'll use later 
--credit to this first function goes to David Walling 
CREATE OR REPLACE FUNCTION dev.beginning_datetime_floor(timestamp without time zone, integer) 
    RETURNS timestamp without time zone AS 
$BODY$ 
SELECT 
date_trunc('minute',timestamp with time zone 'epoch' + 
    floor(extract(epoch from $1)/($2*60))*$2*60 
* interval '1 second') at time zone 'CST6CDT' 
$BODY$ 
    LANGUAGE sql VOLATILE; 

--the following function is what I described on my previous post 
CREATE OR REPLACE FUNCTION dev.round_minutes(timestamp without time zone, integer) 
    RETURNS timestamp without time zone AS 
$BODY$ 
    SELECT date_trunc('hour', $1) + cast(($2::varchar||' min') as interval) * round(date_part('minute',$1)::float/cast($2 as float)) 
$BODY$ 
    LANGUAGE sql VOLATILE; 

--let's load the data into a temp table, I added some data points. note: i got rid of the partial seconds 
SELECT cast(timestamp_original as timestamp) as timestamp_original, datapoint INTO TEMPORARY TABLE timestamps_second2 
FROM 
(
SELECT '2007-09-14 22:56:12' as timestamp_original, 0 as datapoint 
UNION 
SELECT '2007-09-14 22:58:12' as timestamp_original, 1 as datapoint 
UNION 
SELECT '2007-09-14 23:00:12' as timestamp_original, 10 as datapoint 
UNION 
SELECT '2007-09-14 23:02:12' as timestamp_original, 100 as datapoint 
UNION 
SELECT '2007-09-14 23:04:12' as timestamp_original, 1000 as datapoint 
UNION 
SELECT '2007-09-14 23:06:12' as timestamp_original, 10000 as datapoint 
) as data 

--this is the bit of code you'll have to replace with your implementation of getting all possible minutes 
--you could make some sequence of timestamps in R, or simply make the timestamps in Excel to test out the rest of the code 
--the result of the query is simply '2007-09-14 00:00:00' through '2007-09-14 23:59:00' 
SELECT * INTO TEMPORARY TABLE possible_timestamps 
FROM 
(
select the_date + beginning_minute as minute_timestamp 
FROM datawarehouse.dim_date as dim_date 
JOIN datawarehouse.dim_time as dim_time 
ON 1=1 
where dim_date.the_date = '2007-09-14' 
group by the_date, beginning_minute 
order by the_date, beginning_minute 
) as data 

--round to nearest minute (be sure to think about how this might change your results 
SELECT * INTO TEMPORARY TABLE rounded_timestamps2 
FROM 
(
SELECT dev.round_minutes(timestamp_original,1) as minute_timestamp_rounded, datapoint 
from timestamps_second2 
) as data 

--let's join what minutes we have data for versus the possible minutes 
--I used some subqueries so when you select all from the table you'll see the important part (not needed) 
SELECT * INTO TEMPORARY TABLE joined_with_possibles 
FROM 
(
SELECT * 
FROM 
(
SELECT *, (MIN(minute_timestamp_rounded) OVER()) as min_time, (MAX(minute_timestamp_rounded) OVER()) as max_time 
FROM possible_timestamps as t1 
LEFT JOIN rounded_timestamps2 as t2 
ON t1.minute_timestamp = t2.minute_timestamp_rounded 
ORDER BY t1.minute_timestamp asc 
) as inner_query 
WHERE minute_timestamp >= min_time 
AND minute_timestamp <= max_time 
) as data 

--here's the tricky part that might not suit your needs, but it's one method 
--if it's missing a value it grabs the previous value 
--if it's missing the prior value it grabs the one before that, otherwise it's null 
--best practice would be run another case statement with 0,1,2 specifying which point was pulled, then you can count those when you aggregate 
SELECT * INTO TEMPORARY TABLE shifted_values 
FROM 
(
SELECT 
*, 
case 
when datapoint is not null then datapoint 
when datapoint is null and (lag(datapoint,1) over (order by minute_timestamp asc)) is not null 
    then lag(datapoint,1) over (order by minute_timestamp asc) 
when datapoint is null and (lag(datapoint,1) over (order by minute_timestamp asc)) is null and (lag(datapoint,2) over (order by minute_timestamp asc)) is not null 
    then lag(datapoint,2) over (order by minute_timestamp asc) 
else null end as last_good_value 
from joined_with_possibles 
ORDER BY minute_timestamp asc 
) as data 

--now we use the function from my previous post to make the timestamps to aggregate on 
SELECT * INTO TEMPORARY TABLE shifted_values_with_five_minute 
FROM 
(
SELECT *, dev.beginning_datetime_floor(minute_timestamp,5) as five_minute_timestamp 
FROM shifted_values 
) as data 

--finally we aggregate 
SELECT 
AVG(datapoint) as avg_datapoint, five_minute_timestamp 
FROM shifted_values_with_five_minute 
GROUP BY five_minute_timestamp