Re:

From: John Sidney-Woollett <johnsw(at)wardbrook(dot)com>
To: Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Cc: "Pgsql-General (E-mail)" <pgsql-general(at)postgresql(dot)org>
Subject: Re:
Date: 2005-01-02 17:22:44
Message-ID: 41D82DE4.60604@wardbrook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Useful to add a title to your messages before you post...

How about:

select parentid, count(*) as number_of_children
from childtable
group by parentid
order by parentid;

If there are parent records that have no children then these will be
omitted. The query above totally ignores the parent table (which you may
not want it to do).

John Sidney-Woollett

Joost Kraaijeveld wrote:

> Hi all,
>
> Is it possible to count and display the number of children of a parent in a generic query?
>
>
> parent table: id
> child table: id, parent_id
>
> Example output of the query:
>
> parentid number_of_children
> parent1 2
> parent2 6
> parent3 0
>
>
>
> Groeten,
>
> Joost Kraaijeveld
> Askesis B.V.
> Molukkenstraat 14
> 6524NB Nijmegen
> tel: 024-3888063 / 06-51855277
> fax: 024-3608416
> e-mail: J(dot)Kraaijeveld(at)Askesis(dot)nl
> web: www.askesis.nl
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly

In response to

  • at 2005-01-02 10:49:18 from Joost Kraaijeveld

Browse pgsql-general by date

  From Date Subject
Next Message Timothy Perrigo 2005-01-02 18:07:32 OS X shared memory problems 8.0rc3
Previous Message Karsten Hilbert 2005-01-02 16:38:24 Re: Large Objects