Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 6DDACB5DBDE for ; Sun, 16 Oct 2011 05:35:41 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 90861-08 for ; Sun, 16 Oct 2011 08:35:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id DF95AB5DBEE for ; Sun, 16 Oct 2011 05:35:30 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RFMBp-0001kH-A0 for pgsql-sql@postgresql.org; Sun, 16 Oct 2011 10:35:25 +0200 Received: from 212.126.222.122 ([212.126.222.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 16 Oct 2011 10:35:25 +0200 Received: from hari.fuchs by 212.126.222.122 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 16 Oct 2011 10:35:25 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: Group by on Date Date: Sun, 16 Oct 2011 10:35:13 +0200 Lines: 37 Message-ID: <86k485tjum.fsf@protecting.net> References: <1318661510830-4904685.post@n5.nabble.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 212.126.222.122 X-Archive: encrypt User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:+WOSG4sdAuOtgsxaopOjTU0s//4= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.229 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_NUMERIC_HELO=1.164, RP_MATCHES_RCVD=-0.504, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201110/26 X-Sequence-Number: 35910 In article <1318661510830-4904685.post@n5.nabble.com>, "maya.more" writes: > 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 SELECT ('2011-10-01'::date + INTERVAL '3 day' * ((date - '2011-10-01') / 3))::date AS start_date, ('2011-10-01'::date + INTERVAL '2 day' + INTERVAL '3 day' * ((date - '2011-10-01') / 3))::date AS end_date, sum(unit) AS unit FROM tbl WHERE date BETWEEN '2011-10-01' AND '2011-10-06' GROUP BY 1, 2 ORDER BY 1