Re: join with an array

Поиск
Список
Период
Сортировка
От Achilleas Mantzios
Тема Re: join with an array
Дата
Msg-id 201002241639.00331.achill@matrix.gatewaynet.com
обсуждение исходный текст
Ответ на Re: join with an array  (Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>)
Список pgsql-sql
Στις Wednesday 24 February 2010 15:34:48 ο/η Louis-David Mitterrand έγραψε:

> Here is a test case I built. I want to list all cruises by cruise_type
> but after merging cruise_type that have the same cruise_type_name:
>
> drop table cruise;
> drop table cruise_type;
>
> create table cruise_type (
>     id_cruise_type serial primary key,
>     cruise_type_name text
> );
>
> create table cruise (
>     id_cruise serial,
>     id_cruise_type integer references cruise_type,
>     cruise_date timestamp default now()
> );
>
> insert into cruise_type (cruise_type_name) values
> ('5 day eastern carribean cruise'),
> ('5 day western carribean cruise'),
> ('5 day eastern carribean cruise'),
> ('5 day western carribean cruise')
> ;
>
> insert into cruise (id_cruise_type) values
> (1),
> (2),
> (3),
> (4),
> (1),
> (2),
> (3),
> (4)
> ;
>
> select array_agg(ct.id_cruise_type),ct.cruise_type_name from cruise_type ct join cruise c on (c.id_cruise =
any(array_agg))group by cruise_type_name; 
>

You dont specify (in english) what you exactly want to achive, but here is my shot:
1st, get the cruises by cruise type:
select ct.id_cruise_type,array_agg(c.id_cruise) as "List of Cruises" from cruise_type ct, cruise c WHERE
c.id_cruise_type=ct.id_cruise_typeGROUP BY ct.id_cruise_type ORDER BY ct.id_cruise_type;id_cruise_type | List of
Cruises
----------------+-----------------             1 | {1,5}             2 | {2,6}             3 | {3,7}             4 |
{4,8}
(4 rows)

test=#
Then you may pretify this to include the name of each cruise type as well:

select ct.id_cruise_type,ct.cruise_type_name,array_agg(c.id_cruise) as "List of Cruises" from cruise_type ct, cruise c
WHEREc.id_cruise_type=ct.id_cruise_type GROUP BY ct.id_cruise_type,ct.cruise_type_name ORDER BY
ct.id_cruise_type;id_cruise_type|        cruise_type_name        | List of Cruises 
----------------+--------------------------------+-----------------             1 | 5 day eastern carribean cruise |
{1,5}            2 | 5 day western carribean cruise | {2,6}             3 | 5 day eastern carribean cruise | {3,7}
      4 | 5 day western carribean cruise | {4,8} 
(4 rows)

EXERCISE:
Why cant we exclude ct.id_cruise_type from the select clause and group by of the above query?

--
Achilleas Mantzios


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

Предыдущее
От: "Ricardo S. Carvalho"
Дата:
Сообщение: Microsoft Sync Framework
Следующее
От: Aron
Дата:
Сообщение: Using new.id in the WHERE condition of one UPDATE inside ON INSERT rule