Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E3FD4634560 for ; Wed, 23 Sep 2009 20:33:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 27084-02 for ; Wed, 23 Sep 2009 23:32:59 +0000 (UTC) X-Greylist: delayed 00:21:46.603832 by SQLgrey-1.7.6 Received: from mail-px0-f195.google.com (mail-px0-f195.google.com [209.85.216.195]) by mail.postgresql.org (Postfix) with ESMTP id 4116B632F4B for ; Wed, 23 Sep 2009 20:33:06 -0300 (ADT) Received: by pxi33 with SMTP id 33so972416pxi.28 for ; Wed, 23 Sep 2009 16:33:04 -0700 (PDT) Received: by 10.115.29.4 with SMTP id g4mr4741730waj.175.1253747057902; Wed, 23 Sep 2009 16:04:17 -0700 (PDT) Received: from ?192.168.230.104? (adsl-71-139-219-78.dsl.snfc21.pacbell.net [71.139.219.78]) by mx.google.com with ESMTPS id 20sm674533pzk.9.2009.09.23.16.04.17 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 23 Sep 2009 16:04:17 -0700 (PDT) From: Kai Carter Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Subject: SQL moving window averages/statistics Date: Wed, 23 Sep 2009 16:04:15 -0700 Message-Id: <35FD937C-FE22-4272-B3C2-AE4B3E58579A@customweather.com> To: pgsql-sql@postgresql.org Mime-Version: 1.0 (Apple Message framework v1075.2) X-Mailer: Apple Mail (2.1075.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.74 tagged_above=-10 required=5 tests=BAYES_20=-0.74 X-Spam-Level: X-Archive-Number: 200909/114 X-Sequence-Number: 33179 Hi there, I'm currently have an sql statement that selects a week of descriptive statistics for various historical weather variables, sorted by date. SELECT to_char(date, 'MM')||to_char(date, 'DD') as date, max(tmax) as max_tmax, min(tmax) as min_tmax, avg(tmax) as mean_tmax, stddev(tmax) as std_tmax, count(tmax) as count_tmax, sum(tmax) as sum_tmax, variance (tmax) as var_tmax FROM daily_climate where icao='KSFO' and (EXTRACT (MONTH from date) =9 and EXTRACT(DAY from date) = 23) or (EXTRACT (MONTH from date) = 9 and EXTRACT(DAY from date) = 24) or (EXTRACT (MONTH from date) = 9 and EXTRACT(DAY from date) = 25) or (EXTRACT (MONTH from date) = 9 and EXTRACT(DAY from date) = 26) or (EXTRACT (MONTH from date) =9 and EXTRACT(DAY from date) = 27) or (EXTRACT (MONTH from date) = 9 and EXTRACT(DAY from date) = 28) or (EXTRACT (MONTH from date) = and EXTRACT(DAY from date) = 29) group by date order by date; The problem is that I only have 36 years of data to work with, and I would prefer to have a sample of ~100 rather than 30. So the idea would be to have a sample statistics for each day made up of 3 days: the current day, the day previous and the day after. Is it possible to get this sort of a result with one select statement? Thanks in advance for your responses, Kai Carter