Re: range intervals in window function frames

From: Vincent Veyron <vv(dot)lists(at)wanadoo(dot)fr>
To: Daniel Popowich <danielpopowich(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: range intervals in window function frames
Date: 2010-12-14 11:12:48
Message-ID: 1292325168.2312.38.camel@asus-1001PX.home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

[sent you a personal email by mistake - slightly corrected query posted
to the list]

Le dimanche 12 décembre 2010 à 10:55 -0500, Daniel Popowich a écrit :
> Hello all!
>
> I need to do moving averages over time series data and was hoping
> window functions could solve the problem for me, but it doesn't look
> like 8.4 or even 9.0 implementations are quite there, yet.
>
> Currently, if I have this table:
>
> create table sample (
> ts timestamp,
> value integer
> );
> create index sample_ts on sample (ts);

> What I would LIKE to do is this:
>
> select *, avg(ts) over(order by ts range (interval '5 min') preceding)
> from sample order by ts;
>

This?

select t1.ts, t1.value, (select avg(t2.value) from (select value from
sample where (t1.ts-ts)::INTERVAL <= interval '5 minutes' and ts<t1.ts)
as t2) from sample t1;

--
Vincent Veyron
http://marica.fr/
Progiciel de gestion des dossiers de contentieux et d'assurance pour le service juridique

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Wim Bertels 2010-12-14 11:15:08 Best GPLike ERD/SQL developing tool for postgresql?
Previous Message Sim Zacks 2010-12-14 10:45:03 Re: crosstab function