Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 395C1B5DBCE for ; Sun, 16 Oct 2011 05:27:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 87098-04 for ; Sun, 16 Oct 2011 08:27:10 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mailout01.ims-firmen.de (mailout01.ims-firmen.de [213.174.32.96]) by mail.postgresql.org (Postfix) with ESMTP id D98F3B5D80B for ; Sun, 16 Oct 2011 05:27:09 -0300 (ADT) Received: from mailin03.ims-firmen.de ([192.168.1.112]) by mailout01.ims-firmen.de with esmtp (envelope-from ) id 1RFM3n-0003OF-CZ for pgsql-sql@postgresql.org; Sun, 16 Oct 2011 10:27:07 +0200 Received: from [87.170.193.79] (helo=a-kretschmer.de) by mailin03.ims-firmen.de with esmtpsa (TLSv1:AES256-SHA:256) (envelope-from ) id 1RFM3n-0007L3-5Y for pgsql-sql@postgresql.org; Sun, 16 Oct 2011 10:27:07 +0200 Received: from kretschmer by a-kretschmer.de with local (Exim 4.69) (envelope-from ) id 1RFM3m-0002go-15 for pgsql-sql@postgresql.org; Sun, 16 Oct 2011 10:27:06 +0200 Date: Sun, 16 Oct 2011 10:27:06 +0200 From: Andreas Kretschmer To: pgsql-sql@postgresql.org Subject: Re: Group by on Date Message-ID: <20111016082705.GB8614@tux> References: <1318661510830-4904685.post@n5.nabble.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1318661510830-4904685.post@n5.nabble.com> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201110/25 X-Sequence-Number: 35909 maya.more wrote: > I have a table with Date and unit column. . I want to find sum of unit column > considering 3 days each > > User will specify start and enddate > > Eg > > Date Unit > 10/1/2011 1 > 10/2/2011 2 > 10/3/2011 3 > 10/4/2011 4 > 10/5/2011 4 > 10/6/2011 1 > 10/7/2011 2 > 10/8/2011 3 > 10/9/2011 1 > 10/10/2011 1 > 10/11/2011 1 > suppose if user selects date 10/1/2011 to 10/6/2011 > > then output should be > > start date enddate unit > 10/1/2011 10/3/2011 6 > 10/4/2011 10/6/2011 9 > > > pls let me know if anybody has any idea. Maybe something like that: test=*# select * from d; d | i ------------+---- 2011-10-17 | 1 2011-10-18 | 2 2011-10-19 | 3 2011-10-20 | 4 2011-10-21 | 5 2011-10-22 | 6 2011-10-23 | 7 2011-10-24 | 8 2011-10-25 | 9 2011-10-26 | 10 2011-10-27 | 11 2011-10-28 | 12 2011-10-29 | 13 2011-10-30 | 14 2011-10-31 | 15 2011-11-01 | 16 2011-11-02 | 17 2011-11-03 | 18 2011-11-04 | 19 2011-11-05 | 20 (20 rows) Time: 0,276 ms test=*# select '2011-01-01'::date + (extract(doy from d) / 3)::int * '3day'::interval - '3day'::interval as "from", '2011-01-01'::date + (extract(doy from d) / 3)::int * '3day'::interval as "to", sum(i) from d group by 1,2 order by 1; from | to | sum ---------------------+---------------------+----- 2011-10-16 00:00:00 | 2011-10-19 00:00:00 | 6 2011-10-19 00:00:00 | 2011-10-22 00:00:00 | 15 2011-10-22 00:00:00 | 2011-10-25 00:00:00 | 24 2011-10-25 00:00:00 | 2011-10-28 00:00:00 | 33 2011-10-28 00:00:00 | 2011-10-31 00:00:00 | 42 2011-10-31 00:00:00 | 2011-11-03 00:00:00 | 51 2011-11-03 00:00:00 | 2011-11-06 00:00:00 | 39 (7 rows) it fails for more than 1 year, but i hope, you can modify my sql for your own. Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknown) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°