Re: Ayuda con Concatenación

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Postgres studio10 <postgres(at)studio10(dot)cl>
Cc: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: Ayuda con Concatenación
Date: 2007-10-07 03:54:38
Message-ID: 20071007035438.GA6609@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Postgres studio10 escribió:

> producto
> -------------------
> 1 polera
> 2 pantalon
>
> atributo
> ------------------
> 11 amarillo
> 22 rojo
> 33 azul
>
> atributo_producto
> ------------------------
> 1 11
> 1 22
> 1 33
>
> ¿Como quedaría la función para poder pasar como argumento el código
> del producto y me retorne todos los atributos pertenecientes a él,
> incluida su descripción?

Recordando
http://svr5.postgresql.org/pgsql-es-ayuda/2007-01/msg00931.php
se puede hacer algo asi:

CREATE OR REPLACE FUNCTION concat_comma(text, text) RETURNS text AS '
BEGIN
IF $1 IS NULL THEN
RETURN $2;
ELSIF $2 IS NULL THEN
RETURN $1;
ELSE
RETURN $1 || '', '' || $2;
END IF;
END;
' called on null input LANGUAGE 'plpgsql';

CREATE AGGREGATE char_comma_sum (basetype = text, sfunc = CONCAT_COMMA,
stype = text);

Luego

SELECT producto.nombre || ' ' || char_comma_sum(atributo.nombre)
FROM producto join atributo_producto using (producto_id) join
atributo using (atributo_id)
where producto.id = 1
group by producto.id

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message usuario anonimo 2007-10-07 04:30:47 Re: Ayuda con Concatenación
Previous Message Postgres studio10 2007-10-07 03:34:35 Ayuda con Concatenación