pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jwieck@debis.com
To: pat@patoche.org
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] problem with join & count
Date: Thu, 4 Feb 1999 22:49:28 +0100 (MET)
Message-ID: <m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de> (raw)
In-Reply-To: <199902042039.VAA00553@vagabond.bde.espci.fr>

> i need an sql query that will return:
>
> name   |  how_often
> -------------------
>  toto  | 2
>  titi  | 1
>  tutu  | 2
>
> that is, the result table should contain all names present in the first table
> and then the number of times the number associated with the name appears in
> the second table.

    Postgres  does not (and v6.5 will not) support outer joins or
    subselects in the targetlist, what's required to do that in a
    single SQL statement.

    What  you  could  do  is using a SQL function that covers the
    count() like this:

        create function num_refs (int4) returns int4 as '
            select count(*) from tab2 where id = $1;
        ' language 'SQL';

        select item_name, num_refs(id) as how_often from tab1;

    I changed 'number' into id and name  into  item_name  because
    both are reserved words.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #





view thread (7+ messages)  latest in thread

Message-ID: <m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de>
Permalink:  ../m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de/
Also on:    postgresql.org/message-id/m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jwieck@debis.com, pat@patoche.org
  Subject: Re: [SQL] problem with join & count
  In-Reply-To: <m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de>
  References: <199902042039.VAA00553@vagabond.bde.espci.fr> <m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox