Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id E2C5A1337B9F for ; Fri, 8 Oct 2010 20:10:07 -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 06275-01 for ; Fri, 8 Oct 2010 23:10:02 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from blu0-omc2-s24.blu0.hotmail.com (blu0-omc2-s24.blu0.hotmail.com [65.55.111.99]) by mail.postgresql.org (Postfix) with ESMTP id 3FDB91337B9B for ; Fri, 8 Oct 2010 20:10:00 -0300 (ADT) Received: from BLU0-SMTP96 ([65.55.111.72]) by blu0-omc2-s24.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 8 Oct 2010 16:09:59 -0700 X-Originating-IP: [174.91.0.143] X-Originating-Email: [fbax@sympatico.ca] Message-ID: Received: from t60p.bax.ca ([174.91.0.143]) by BLU0-SMTP96.blu0.hotmail.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Fri, 8 Oct 2010 16:09:58 -0700 Date: Fri, 8 Oct 2010 19:09:56 -0400 From: Frank Bax User-Agent: Thunderbird 2.0.0.23 (X11/20100309) MIME-Version: 1.0 CC: pgsql-sql@postgresql.org Subject: Re: counting related rows References: In-Reply-To: Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 08 Oct 2010 23:09:58.0689 (UTC) FILETIME=[ED960910:01CB673D] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.878 tagged_above=-5 required=5 tests=BAYES_00=-1.9, MISSING_HEADERS=1.021, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201010/26 X-Sequence-Number: 34677 James Cloos wrote: > I have a table which includes a text column containing posix-style > paths. Ie, matching the regexp "^[^/]+(/[^/]+)*$". > > I need to do a query of a number of columns from that table, plus the > count of rows which are "children" of the current row. > > The query: > > SELECT count(*) AS nch FROM m WHERE o = (SELECT o FROM m WHERE id=30016) > AND name ILIKE (SELECT name || '/%' FROM m WHERE id=30016); > > selects that extra column given the id. > > A view containing all of the columns from m plus a column matching the > above select would cover my needs well. > > But I haven't been able to get the syntax right. It would help if you provided: a) statements to create sample data b) expected results from sample data Does this do what you want? select * from m, (SELECT count(*) AS nch FROM m WHERE o = (SELECT o FROM m WHERE id=30016) AND name ILIKE (SELECT name || '/%' FROM m WHERE id=30016)) om;