X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 5F33F9DCC83 for ; Wed, 21 Dec 2005 20:59:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 58031-03 for ; Wed, 21 Dec 2005 20:59:34 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mulva.getbusi.com (getbusi-dmz.intellinc.utas.edu.au [202.12.92.169]) by postgresql.org (Postfix) with ESMTP id AF5DA9DCC80 for ; Wed, 21 Dec 2005 20:59:27 -0400 (AST) Received: from getbusi8.intellinc.utas.edu.au (getbusi8.intellinc.utas.edu.au [202.12.92.66]) by mulva.getbusi.com (8.13.1/8.13.1) with ESMTP id jBM0xOqm005328; Thu, 22 Dec 2005 11:59:25 +1100 From: Matthew Smith To: Richard Huxton Subject: Re: Help on a complex query (avg data for day of the week) Date: Thu, 22 Dec 2005 11:59:24 +1100 User-Agent: KMail/1.5.3 Cc: pgsql-sql@postgresql.org References: <200512191657.48139.mps@utas.edu.au> <43A928C2.8060901@archonet.com> In-Reply-To: <43A928C2.8060901@archonet.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200512221159.24388.mps@utas.edu.au> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.08 required=5 tests=[AWL=0.080] X-Spam-Score: 0.08 X-Spam-Level: X-Archive-Number: 200512/195 X-Sequence-Number: 23737 Richard (and list), Thanks for the help! More below: On Wed, 21 Dec 2005 09:04 pm, Richard Huxton wrote: > Matthew Smith wrote: > > I want to form a query that returns the average total usage for each day > > of the week, eg: > > [snip] > > > To get this info, I am using the following query: > > > > select dow as day, sum(sum_data)/count(dow) as avg_usage from > > (select extract('dow' from date_trunc('day', time)) as dow, sum(data) as > > sum_data > > from datalog where time >= '2005-09-11' and time <= '2005-09-25' group by > > dow) as avg_data_per_day group by day; > > > > This works well, assuming that there is at least one entry in the table > > for each day in the time period. > > > > The problem comes when there are days where no data is logged. > > 1. Calculate how many data-points each day represents > 2. Sum the days you do have data for > 3. Left-join #1 to #2 so you can calculate the average. > > I'm guessing for #1 you'd rather not have 7 UNIONs, so you might find > generate_series() a useful function. See Ch 9.18. Set Returning Functions. Sadly I am using 7.3, it seems that generate_series() is 8.0 and later. I am not sure what you meant by "Calculate how many data-points each day represents", but your mention of unions gave me an idea. It's nasty, but it will work for what I am trying to do (sorry if it is what you meant): I can use unions to make a list of dates and left join them into my query to get the missing days in. This would be the same as left joining in the result from generate_series(), but it is not as tidy. I will make the list using something like this: select '2005-09-11'::timestamp as date UNION select '2005-09-12'::timestamp as date UNION select '2005-09-13'::timestamp as date UNION etc. It's nasty, but it will hold until I can migrate to 8.1. Thanks again for the help! Cheers, Matthew Smith