Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 98294632703 for ; Fri, 9 Apr 2010 07:52:21 -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 96637-06 for ; Fri, 9 Apr 2010 10:52:10 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 1DC066324E2 for ; Fri, 9 Apr 2010 07:52:11 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1O0Bol-0003ZE-5n for pgsql-sql@postgresql.org; Fri, 09 Apr 2010 12:52:07 +0200 Received: from srv.protecting.net ([212.126.218.242]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 09 Apr 2010 12:52:07 +0200 Received: from hari.fuchs by srv.protecting.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 09 Apr 2010 12:52:07 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: Cascading sum in tree with CTE? Date: Fri, 09 Apr 2010 12:51:58 +0200 Lines: 59 Message-ID: References: <4BBED49D.7080501@krap.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: srv.protecting.net X-Archive: encrypt User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:6kf29MY8JRDE2xENOXx0MUCmbtc= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201004/36 X-Sequence-Number: 33963 In article <4BBED49D.7080501@krap.dk>, Svenne Krap writes: > Hi . > My problem resembles this: > I have the following (simplified) tables > 1) create table account ( id serial, name varchar, parent_id int4 > references account, primary key (id)) > 2) create table transaction (id serial, account_id int4 references > account, memo varchar, debet, credit, primary key(id)) > So a basic ledger system, with a tree of accounts and transactions on > them. Some accounts have transactions others doesn't. > I have written a CTE which generates the tree, but I would like to > append to each line the sum of all debits and credits of this and all > sub accounts. > I think it sould be doable, but cannot bend my brain around it and my > google-fu has come out short. > Say the data was: > Account: > 1, 'Expenses', null > 2, 'IRS', 1 > 3, '7-Eleven' > Transaction: > 1, 2, 'Tax Jan 10', null, 100 > 2, 2, 'Tax Feb 10', null, 120 > 3, 2, 'Tax Feb 10 correction', 10,null > 4, 3, 'Sodas', 10, null > 5, 3, 'Beer', 5, null > I would expect a tree like: > 1, Debt, 25, 220 > 2, IRS, 10, 220 > 3, 7-eleven, 15, 0 > Is there any way around a writing a stored procedure for that? How about an ancestor table? WITH RECURSIVE tree (id, anc) AS ( SELECT id, id FROM account UNION ALL SELECT a.id, t.anc FROM account a JOIN tree t ON t.id = a.parent_id ) SELECT a.id, a.name, sum(x.debet) AS debet, sum(x.credit) AS credit FROM account a JOIN tree t ON t.anc = a.id LEFT JOIN transaction x ON x.account_id = t.id GROUP BY a.id, a.name