Google

Kategorie

Reprezentacja Wiedzy

Kalendarz

June 2007
M T W T F S S
« May   Jul »
 123
45678910
11121314151617
18192021222324
252627282930  

GROUP_CONCAT w PostgreSQL

June 6th, 2007 by prond

Pisałem wcześniej o bardzo przyjemnym feature MySQL’a, jakim jest funkcja agregująca GROUP_CONCAT
Niestety brakuje tej funkcji w PostgreSQL, ale to nie stanowi większego problemu ponieważ w postgre’sie mamy na prawdę potężne możliwości w dodawaniu nowej funkcjonalności. W przypadku GROUP_CONCAT stworzyłem agregat:

Krok I : funkcja stanu

CREATE OR REPLACE FUNCTION fun_group_concat(text, text)
  RETURNS text AS
$BODY$
BEGIN
    RETURN $1||$2;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

Krok II : agregat

CREATE AGGREGATE group_concat(
  BASETYPE=text,
  SFUNC=public.fun_group_concat,
  STYPE=text
);

Krok III : kwerenda

SELECT trim(BOTH ', ' FROM group_concat(name||', ')) FROM tags;

Posted in PostgreSQL |

One Response

  1. prond Says:

    No cóż, przekomplikowałem w tym artykule. Można to zrobić znacznie, znacznie prościej:

    SELECT array_to_string(array(SELECT name FROM tags ),’, ‘);

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.