Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id AA3E6632509 for ; Wed, 14 Apr 2010 13:07:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 19611-03 for ; Wed, 14 Apr 2010 16:07:00 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wy0-f174.google.com (mail-wy0-f174.google.com [74.125.82.174]) by mail.postgresql.org (Postfix) with ESMTP id AA31163242E for ; Wed, 14 Apr 2010 13:07:00 -0300 (ADT) Received: by wyf19 with SMTP id 19so139431wyf.19 for ; Wed, 14 Apr 2010 09:06:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:content-type :content-transfer-encoding; bh=Fn2/xjA8N4M73dj31EPAaLVaM0XB2JQkbv9Jt7T5hnQ=; b=B/EuPl1EH4JFfO7x9Sj+5xQ7OhYggbjZ33HwWbkGfnzuqxJ7PuZxl1/pXclF6c7Lzb 2+k3cphNZ/h30+Oqi+45GNX55e0E40E/JlbpMAydEVLUODB6b4BSpWKdd+wH1sHdS4iJ BG+dfAcO4unoe+iEUrGZQKmhyAia2xTCg3iQg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=KXrOQ/HjPRY+BIVRP0c7AVM4SoKoq6MEhGueMXVq15fT0NGCe50+6RPkcb4cBrWrNk OwEwU3gDKRXscD/eImE1B+njm/ORnUHlfm7a9VhI2Un36yw40FceqDSe/hkuVeVATSrD +GnzcHvkEWEkIBakerX//9EF7pt5LP2QqvFgs= MIME-Version: 1.0 Received: by 10.216.233.151 with HTTP; Wed, 14 Apr 2010 09:06:59 -0700 (PDT) In-Reply-To: <20100414154958.GA16847@apartia.fr> References: <20100414145434.GA13682@apartia.fr> <20100414154958.GA16847@apartia.fr> Date: Wed, 14 Apr 2010 18:06:59 +0200 Received: by 10.216.188.77 with SMTP id z55mr4946502wem.3.1271261219538; Wed, 14 Apr 2010 09:06:59 -0700 (PDT) Message-ID: Subject: Re: graphing time series data From: Pavel Stehule To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.111 tagged_above=-10 required=5 tests=AWL=-0.211, BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201004/54 X-Sequence-Number: 33981 2010/4/14 Louis-David Mitterrand : > 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. =C2=A0Only I was >> calculating the counts per day. =C2=A0this query could be crafted to wor= k >> for you. >> >> CREATE OR REPLACE VIEW opendiscrepencydailycounts AS >> =C2=A0WITH opendays(day) AS ( >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0SELECT gs.day::date AS day >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0FROM generate_series((( SELECT >> min(discrepencylist.discstartdt) AS min >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0FRO= M discrepencylist))::timestamp without time >> zone, 'now'::text::date::timestamp without time zone, '1 >> day'::interval) gs(day) >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 ) >> =C2=A0SELECT opendays.day, ds.resolvingparty, count(opendays.day) AS >> opendiscrepancies >> =C2=A0 =C2=A0FROM discrepencylist ds, opendays >> =C2=A0 WHERE opendays.day >=3D ds.discstartdt AND opendays.day <=3D >> LEAST('now'::text::date, ds.resolutiondate) >> =C2=A0 GROUP BY opendays.day, ds.resolvingparty >> =C2=A0 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 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(select generate_s= eries(min(p.created_on),max(p.modified_on),'1 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0week') as week fro= m price p) as w join price p on (p.created_on < > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0w.week + '7 days' = and p.modified_on > w.week + '7 days') group by > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0w.week order by w.= week > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0week =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | count > ------------------------+-------- > =C2=A02010-02-10 15:32:18+01 | 125369 > =C2=A02010-02-17 15:32:18+01 | 126882 > =C2=A02010-02-24 15:32:18+01 | 128307 > =C2=A02010-03-03 15:32:18+01 | 126742 > =C2=A02010-03-10 15:32:18+01 | 133596 > =C2=A02010-03-17 15:32:18+01 | 149019 > =C2=A02010-03-24 15:32:18+01 | 149908 > =C2=A02010-03-31 15:32:18+02 | 147617 > > The rest should be easy from there! I don't understand well. Why you don't use a function date_trunc(), select date_trunc('week', created), count(*) from price group by date_trunc('week', created) Regards Pavel Stehuke > > Thanks for your input, > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >