Resp.: text array accumulate to multidimensional text array

Поиск
Список
Период
Сортировка
От Osvaldo Kussama
Тема Resp.: text array accumulate to multidimensional text array
Дата
Msg-id 690707f60810220955j7aa443e1nd0ab2771ad780cfa@mail.gmail.com
обсуждение исходный текст
Ответ на text array accumulate to multidimensional text array  ("Rainer Zaiss" <r.zaiss@free.fr>)
Ответы Re: text array accumulate to multidimensional text array  ("Merlin Moncure" <mmoncure@gmail.com>)
Список pgsql-general
2008/10/14, Rainer Zaiss <r.zaiss@free.fr>:
>
> I would like to aggregate a text array into a multidimensional text array.
>
> Let us say I have one table with two collumns
>
> ID    ARRAY
> A    {"A1","B1","C1"}
> A    {"A2","B2","C2"}
> B     {"A3","B3","C3"}
>
> If I use a GROUP BY ID, I would like to receive following result:
>
> ID  ARRAY
> A  {{"A1","B1","C1"},{"A2","B2","C2"}}
> B  {{"A3","B3","C3"}}
>
> I searched around but I didn't find any solution
>


Try:

bdteste=# CREATE OR REPLACE FUNCTION array_cat1(p1 anyarray, p2
anyarray) RETURNS anyarray AS $$
bdteste$# BEGIN
bdteste$#    IF  p1 = '{}'::text[] THEN
bdteste$#       RETURN(ARRAY[p2]);
bdteste$#    ELSE
bdteste$#       RETURN(ARRAY_CAT(p1, p2));
bdteste$#    END IF;
bdteste$# END;
bdteste$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
bdteste=# CREATE AGGREGATE array_accum3(anyarray)
(
   sfunc = array_cat1,
   stype = anyarray,
   initcond = '{}'
);
CREATE AGGREGATE
bdteste=# CREATE TEMP TABLE foo(
bdteste(#    id   char(1),
bdteste(#    a    text[]);
CREATE TABLE
bdteste=# INSERT INTO foo VALUES('A', '{"A1","B1","C1"}');
INSERT 0 1
bdteste=# INSERT INTO foo VALUES('A', '{"A2","B2","C2"}');
INSERT 0 1
bdteste=# INSERT INTO foo VALUES('B', '{"A3","B3","C3"}');
INSERT 0 1
bdteste=# SELECT * FROM foo;
 id |     a
----+------------
 A  | {A1,B1,C1}
 A  | {A2,B2,C2}
 B  | {A3,B3,C3}
(3 registros)

bdteste=# SELECT id, array_accum3(a) FROM foo GROUP BY id;
 id |      array_accum3
----+-------------------------
 B  | {{A3,B3,C3}}
 A  | {{A1,B1,C1},{A2,B2,C2}}
(2 registros)

Osvaldo

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

Предыдущее
От: Goboxe
Дата:
Сообщение: Re: How to view user defined TYPE
Следующее
От: "Andrus"
Дата:
Сообщение: Re: Shopping cart