Re: [SQL] problem with join & count

Поиск
Список
Период
Сортировка
От jwieck@debis.com (Jan Wieck)
Тема Re: [SQL] problem with join & count
Дата
Msg-id m108WeP-000EBPC@orion.SAPserv.Hamburg.dsh.de
обсуждение исходный текст
Ответ на problem with join & count  (pat@patoche.org)
Список pgsql-sql
> 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) #

В списке pgsql-sql по дате отправления:

Предыдущее
От: Michael Olivier
Дата:
Сообщение: Re: [SQL] keeping OID's when copying table
Следующее
От: Bruce Momjian
Дата:
Сообщение: Re: [SQL] keeping OID's when copying tableu