Re: query problem - get count in related table - Mailing list pgsql-novice

From Manfred Koizar
Subject Re: query problem - get count in related table
Date
Msg-id 4dtseuon8a5dvp0cuurmr4vth30e8chim6@4ax.com
Whole thread Raw
In response to Re: query problem - get count in related table  (Rory Campbell-Lange <rory@campbell-lange.net>)
List pgsql-novice
On Fri, 24 May 2002 00:14:59 +0100, Rory Campbell-Lange
<rory@campbell-lange.net> wrote:

>I'm still looking to
>get a result like this:
>
>         id | data | count
>        ----+------+-------
>          1 |    2 |     3
>          2 |    1 |     2
>          3 |    4 |     1
>          5 |    2 |     0
>        (3 rows)
>
>brandf=# select * from a;    brandf=# select * from b;
> id | data                    id
>----+------                  ----
>  1 |    2                     2
>  2 |    1                     2
>  3 |    4                     1
>  5 |    2                     1
>(4 rows)                       1
>                               3
>                             (6 rows)
Rory,

assuming all your a.id are NOT NULL and unique:

    SELECT a.id, a.data, count(b.id)
    FROM a LEFT JOIN b ON a.id = b.id
    GROUP BY a.id, a.data;

HTH.
Servus
 Manfred

pgsql-novice by date:

Previous
From: Tom Ansley
Date:
Subject: Re: Using CASE with a boolean value
Next
From: Tom Lane
Date:
Subject: Re: Using CASE with a boolean value