| From: | Alvaro Herrera <alvherre(at)commandprompt(dot)com> |
|---|---|
| To: | manuel lamas <manuel3w(at)hotmail(dot)com> |
| Cc: | pgsql-es-ayuda(at)postgresql(dot)org |
| Subject: | Re: Lista de valores |
| Date: | 2007-08-13 18:34:16 |
| Message-ID: | 20070813183416.GH17177@alvh.no-ip.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-es-ayuda |
manuel lamas escribió:
> Hola lista,
>
> Digamos que tengo una tabla asi:
>
> CREATE TABLE T1
> (
> productoID int,
> color varchar(20)
> );
>
>
> INSERT INTO T1(productoID,color)VALUES(1,'blanco');
> INSERT INTO T1(productoID,color)VALUES(1,'negro');
> INSERT INTO T1(productoID,color)VALUES(1,'rojo');
>
> INSERT INTO T1(productoID,color)VALUES(2,'blanco');
> INSERT INTO T1(productoID,color)VALUES(2,'negro');
> INSERT INTO T1(productoID,color)VALUES(2,'rojo');
>
> Quiero hacer un SELECT que me de este resultado sin utilizar una función
> (en un simple SELECT), es posible?
>
> 1 blanco,negro,rojo
> 2 blanco,negro,rojo
Puedes usar una funcion de agregacion:
alvherre=# create function concat_with_comma (text, text) returns text language plpgsql called on null input as $$ begin if $1 is null then return $2; end if; if $2 is null then return $1; end if; return $1||','||$2 ; end $$;
CREATE FUNCTION
alvherre=# create AGGREGATE commacat (basetype = text, sfunc = concat_with_comma, stype = text);
CREATE AGGREGATE
alvherre=# select productoid, commacat(color) from t1 group by productoid;
productoid | commacat
------------+-------------------
2 | blanco,negro,rojo
1 | blanco,negro,rojo
(2 rows)
--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Alvaro Herrera | 2007-08-13 20:24:02 | Re: No entiendo que pasa??? |
| Previous Message | Alvaro Herrera | 2007-08-13 17:29:54 | Re: ERROR: Document processed/posted en DBLINK |