From: | "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Moving avg using SQL |
Date: | 2009-09-04 06:03:36 |
Message-ID: | 20090904060336.GA8068@a-kretschmer.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
In response to ????????? :
> How can I do a moving avg by only using SQL?
Which version do you have? Since 8.4 we have CTE aka windowing
functions, a simple axample:
test=*# select n, last_value(n) over mywin , avg(n) over mywin
from generate_series(1,20) n
window mywin as (partition by (n-1)/4 rows between unbounded preceding
and unbounded following);
n | last_value | avg
----+------------+---------------------
1 | 4 | 2.5000000000000000
2 | 4 | 2.5000000000000000
3 | 4 | 2.5000000000000000
4 | 4 | 2.5000000000000000
5 | 8 | 6.5000000000000000
6 | 8 | 6.5000000000000000
7 | 8 | 6.5000000000000000
8 | 8 | 6.5000000000000000
9 | 12 | 10.5000000000000000
10 | 12 | 10.5000000000000000
11 | 12 | 10.5000000000000000
12 | 12 | 10.5000000000000000
13 | 16 | 14.5000000000000000
14 | 16 | 14.5000000000000000
15 | 16 | 14.5000000000000000
16 | 16 | 14.5000000000000000
17 | 20 | 18.5000000000000000
18 | 20 | 18.5000000000000000
19 | 20 | 18.5000000000000000
20 | 20 | 18.5000000000000000
(20 rows)
Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
From | Date | Subject | |
---|---|---|---|
Next Message | Robert Dörfler | 2009-09-04 06:16:41 | Re: N + 1 replication |
Previous Message | Pavel Stehule | 2009-09-04 05:53:26 | Re: How to stop a query |