Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 0949C9FA3CC for ; Wed, 15 Aug 2007 00:20:19 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 66827-08 for ; Wed, 15 Aug 2007 00:20:14 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.191]) by postgresql.org (Postfix) with ESMTP id 489FC9FA2D6 for ; Wed, 15 Aug 2007 00:20:14 -0300 (ADT) Received: by nf-out-0910.google.com with SMTP id f5so977271nfh for ; Tue, 14 Aug 2007 20:20:13 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=j+D+UExFGZg3E5unHQ/ABn1lhsz2lk8iGZdA67Ss4Iu3flnDNy4HK9fBR0308x0HzLY8fkXFMvuXSRw8CusSbYo2dhS4++rzC0AHGfv/gSGb6TFbxlbQ/Mmn2Tyt0wmhtpm07IL/ssaWBKnuhcOpnUatx+NK0PJHLlkhAX2Kv8M= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=mpOQGdKtZU2wiCdkrZEUnsPz2IpBAxj7uwBXqGx2CL7fuye+NZIoGAtPtllgiS261LRoHvpaKkV3aCquKztgr6ySXZdcVh7X2+GUpT+ezlGcF7ZK+541YWPr8b693/y5eos3MlGWEK2T2kqeb/HYT3aiDbC6XgTu8j2cVtigVqs= Received: by 10.78.123.5 with SMTP id v5mr15831huc.1187148012058; Tue, 14 Aug 2007 20:20:12 -0700 (PDT) Received: by 10.78.107.5 with HTTP; Tue, 14 Aug 2007 20:20:12 -0700 (PDT) Message-ID: Date: Tue, 14 Aug 2007 21:20:12 -0600 From: "Josh Tolley" To: "Kirk Wythers" Subject: Re: query help Cc: pgsql-general@postgresql.org In-Reply-To: <97C91D0E-A987-42C5-9DD9-8F96E1298F34@umn.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <97C91D0E-A987-42C5-9DD9-8F96E1298F34@umn.edu> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/771 X-Sequence-Number: 118620 On 8/14/07, Kirk Wythers wrote: > > I need some help with rewriting a query. I have a fairly complicated query > (for me anyway) that dumps daily climate data, filling in missing data with > monthly averages (one line per day). > > I want to output monthly averages (one line per month). I am having a hard > time wrapping my head around this. Particularly how to deal with the doy > column (day of year). I have tried several approaches and my forehead is > starting to get my keyboard bloody. I think this came up on IRC today, so perhaps this is only for the archives' sake, but you want to do something like this: Assuming you have a table as follows: CREATE TABLE climate_data ( measurement_time timestamp, measurement_value integer); ...and you insert data into it regularly, you can get the average measurement over a period of time with date_trunc(), which will truncate a date or timestamp value to match whatever precision you specify. For example, see the following: eggyknap=# select date_trunc('month', now()); date_trunc ------------------------ 2007-08-01 00:00:00-06 (1 row) Note: the -06 at the end means I'm in mountain time. So if you want to get the average measurement over a month's time, you need to do something like this: SELECT DATE_TRUNC('MONTH', measurement_time), AVG(measurement_value) FROM climate_data GROUP BY DATE_TRUNC('MONTH', measurement_time); This will chop all the measurement_time values down to the month the measurement was taken in, put all measurements in groups based on the resulting value, and take the average measurement_value from each group. - Josh