Обсуждение: select by groups

Поиск
Список
Период
Сортировка

select by groups

От
Martin Marques
Дата:
I have a table with names of people, email address, etc, and an identifier
that tells me which group they are in (could be a 1, 2, or 3 person group).
Is it posible to make a query that would give me the name of the persons of
each group in one row? Or do I have to do PL?

-- 19:15:01 up 97 days,  1:24,  4 users,  load average: 0.00, 0.07, 0.17
-----------------------------------------------------------------
Martín Marqués        | select 'mmarques' || '@' || 'unl.edu.ar'
Centro de Telematica  |  DBA, Programador, Administrador            Universidad Nacional                 del Litoral
-----------------------------------------------------------------



Re: select by groups

От
Manuel Sugawara
Дата:
Martin Marques <martin@bugs.unl.edu.ar> writes:

> I have a table with names of people, email address, etc, and an identifier 
> that tells me which group they are in (could be a 1, 2, or 3 person group). 
> Is it posible to make a query that would give me the name of the persons of 
> each group in one row? Or do I have to do PL?

Use GROUP BY with an aggregator that concatenate the names. The
aggregator may be implemented in plpgsql, for instance:

CREATE OR REPLACE FUNCTION concat (text, text) RETURNS text   AS 'select case when $1 = '''' then $2 else ($1 || '', ''
||$2) end'   LANGUAGE sql;
 
CREATE AGGREGATE concat (   BASETYPE = text,   SFUNC = public.concat,   STYPE = text,   INITCOND = ''
);

Regards,
Manuel.