Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 5AECA632B3B for ; Fri, 2 Oct 2009 02:29:30 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 35073-06 for ; Fri, 2 Oct 2009 05:29:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mail.postgresql.org (Postfix) with ESMTP id 5ACC7633251 for ; Fri, 2 Oct 2009 02:29:19 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mreu1) with ESMTP (Nemesis) id 0LsMfM-1MDkZw3zfU-012ZTm; Fri, 02 Oct 2009 07:29:17 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1Mtahg-0006Ri-00 for pgsql-sql@postgresql.org; Fri, 02 Oct 2009 07:29:16 +0200 Date: Fri, 2 Oct 2009 07:29:16 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Need magic for a moving statistic Message-ID: <20091002052916.GA22496@a-kretschmer.de> References: <4AC55231.2080008@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4AC55231.2080008@gmx.net> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1/QuLj/CIztZD9Iz/HksXN9d+lZiYLoJMR0pYk NAcQ+V8AaiyaLAIJsVKx5uCsrNRkhslxUG6Ciqw5do8OEn0y0a WaL8HRcHvzFZmRE8wDD4g== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.3 tagged_above=-10 required=5 tests=AWL=0.299, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/9 X-Sequence-Number: 33208 In response to Andreas : > Hi, > > I need some magic for a moving statistic that works on a rather big > table starting at a given date within the table up until now. > The statistic will count events allways on fridays over periods of 2 > weeks before ... biweekly? > So I'd like to get a line every 2 weeks for everthing between. > > I sadly don't know how to spell that does: > > collect data where insert_date between friday1 and friday1 + > inteval '2 week' > collect data where insert_date between friday1 + inteval '2 week' > + inteval '1 second' and friday1 + inteval '4 week' > collect data where insert_date between friday1 + inteval '4 week' > + inteval '1 second' and friday1 + inteval '6 week' > > Is this possible ? I hope i understand you correctly: test=*# select * from foo; d | value ------------+------- 2009-10-02 | 1 2009-10-03 | 2 2009-10-10 | 3 2009-10-16 | 4 2009-10-20 | 5 2009-10-23 | 6 2009-10-30 | 7 2009-11-05 | 8 2009-11-13 | 9 2009-11-20 | 10 (10 rows) test=*# select ('2009-10-02'::date + period*('14 days'::interval))::date::text || ' bis ' || ('2009-10-02'::date + (period+1)*('14 days'::interval))::date::text, sum from ( select ((d-'2009-10-02'::date)/14) period, sum(value) from foo group by 1 ) foo order by period; ?column? | sum ---------------------------+----- 2009-10-02 bis 2009-10-16 | 6 2009-10-16 bis 2009-10-30 | 15 2009-10-30 bis 2009-11-13 | 15 2009-11-13 bis 2009-11-27 | 19 (4 rows) Or simpler: test=*# select ((d-'2009-10-02'::date)/14) || '. period' as period, sum(value) from foo group by 1; period | sum -----------+----- 0. period | 6 1. period | 15 3. period | 19 2. period | 15 (4 rows) HTH, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)