Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 5F19D633B78 for ; Fri, 2 Oct 2009 00:52:15 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 93641-08 for ; Fri, 2 Oct 2009 03:52:07 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mail.postgresql.org (Postfix) with SMTP id 3225A632428 for ; Fri, 2 Oct 2009 00:52:07 -0300 (ADT) Received: (qmail invoked by alias); 02 Oct 2009 03:52:04 -0000 Received: from unknown (EHLO [192.168.1.113]) [88.130.205.166] by mail.gmx.net (mp001) with SMTP; 02 Oct 2009 05:52:04 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX1++bCdUExG+kMTJuL3KgOOTDAa7PIVX7dCAWsShWA wyntWX2t1qOIoW Message-ID: <4AC57917.2020302@gmx.net> Date: Fri, 02 Oct 2009 05:52:55 +0200 From: Andreas User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: Kai Carter CC: pgsql-sql@postgresql.org Subject: Re: SQL moving window averages/statistics References: <35FD937C-FE22-4272-B3C2-AE4B3E58579A@customweather.com> In-Reply-To: <35FD937C-FE22-4272-B3C2-AE4B3E58579A@customweather.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.51 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/8 X-Sequence-Number: 33207 Just 3 points ... 1) don't use "date" as a column name because it's a data type. 2) to_char(current_date, 'MM')||to_char(current_date, 'DD') is equivalent to to_char(current_date, 'MMDD') 3) you should get the same result with ... where icao='KSFO' and (EXTRACT (MONTH from date) = 9) and (EXTRACT (DAY from date) BETWEEN 23 AND 29)) group by ... Then you lost me with your 3 day idea =8-} It might be depressingly slow but depending how time critical the report is, you could do something like select distinct (date) date, (select max(dc1.tmax) from daily_climate as dc1 where dc1.date between (dc0.date - interval '1 day') and (dc0.date + interval '1 day')) as max_tmax, (select min(dc1.tmax) from daily_climate as dc1 where dc1.date between (dc0.date - interval '1 day') and (dc0.date + interval '1 day')) as min_tmax, ................. from daily_climate as dc0 ............ That's just something that might get you a result. I didn't try it out. Kai Carter schrieb: > 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 >