Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id DCC5763267F for ; Wed, 14 Apr 2010 13:00:14 -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 55127-08-2 for ; Wed, 14 Apr 2010 16:00:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sargon.lncsa.com (mail.lncsa.com [213.215.28.11]) by mail.postgresql.org (Postfix) with ESMTP id F20AF6339FB for ; Wed, 14 Apr 2010 13:00:03 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 9A6313023120 for ; Wed, 14 Apr 2010 18:00:02 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at lncsa.com Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5wkVtEWWGy46 for ; Wed, 14 Apr 2010 18:00:02 +0200 (CEST) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id 6E1123022A50 for ; Wed, 14 Apr 2010 18:00:02 +0200 (CEST) Received: from galba.apartia.fr (galba.apartia.fr [10.0.3.119]) by zenon.apartia.fr (Postfix) with ESMTP id 7CF3C5B940324 for ; Wed, 14 Apr 2010 17:49:58 +0200 (CEST) Received: by galba.apartia.fr (Postfix, from userid 1000) id 5850E1D723; Wed, 14 Apr 2010 17:49:58 +0200 (CEST) Date: Wed, 14 Apr 2010 17:49:58 +0200 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: graphing time series data Message-ID: <20100414154958.GA16847@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20100414145434.GA13682@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201004/53 X-Sequence-Number: 33980 On Wed, Apr 14, 2010 at 08:46:13AM -0700, Richard Broersma wrote: > On Wed, Apr 14, 2010 at 7:54 AM, Louis-David Mitterrand > wrote: > > > Now, I'd like to make a graph of average prices per week, per > > id_product. As some prices don't vary much, distribution would not be > > ideal if I simply 'group by extract(week from p.modified)'. > > I created a view for a similar problem that I had. Only I was > calculating the counts per day. this query could be crafted to work > for you. > > CREATE OR REPLACE VIEW opendiscrepencydailycounts AS > WITH opendays(day) AS ( > SELECT gs.day::date AS day > FROM generate_series((( SELECT > min(discrepencylist.discstartdt) AS min > FROM discrepencylist))::timestamp without time > zone, 'now'::text::date::timestamp without time zone, '1 > day'::interval) gs(day) > ) > SELECT opendays.day, ds.resolvingparty, count(opendays.day) AS > opendiscrepancies > FROM discrepencylist ds, opendays > WHERE opendays.day >= ds.discstartdt AND opendays.day <= > LEAST('now'::text::date, ds.resolutiondate) > GROUP BY opendays.day, ds.resolvingparty > ORDER BY opendays.day, ds.resolvingparty; You confirm my modus operandi. I tried the following which seems to give me optimal price distribution: select w.week,count( p.id_price) from (select generate_series(min(p.created_on),max(p.modified_on),'1 week') as week from price p) as w join price p on (p.created_on < w.week + '7 days' and p.modified_on > w.week + '7 days') group by w.week order by w.week week | count ------------------------+-------- 2010-02-10 15:32:18+01 | 125369 2010-02-17 15:32:18+01 | 126882 2010-02-24 15:32:18+01 | 128307 2010-03-03 15:32:18+01 | 126742 2010-03-10 15:32:18+01 | 133596 2010-03-17 15:32:18+01 | 149019 2010-03-24 15:32:18+01 | 149908 2010-03-31 15:32:18+02 | 147617 The rest should be easy from there! Thanks for your input,