Re: Unir varias tablas en un solo registro

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: MIGUEL CANCHAS <mcanchas(at)tsr(dot)com(dot)pe>
Cc: "'pgsql-es-ayuda(at)postgresql(dot)org'" <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Unir varias tablas en un solo registro
Date: 2008-03-27 23:40:52
Message-ID: 20080327234052.GZ8764@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

MIGUEL CANCHAS escribió:

> Asi deberia de quedar una combinacion.
> idficha tela
> 125 JERSEY SIMPLE 20X 19/1 COC 50X 12/1 ALGOD. OPEN 30X 24/75 COC
>
> Como podria empezar a hacerlo y si podria tenerlo en una vista seria mucho
> mejor, aunque viendolo bien creo que solo podria ser con una funcion

Es muy facil. Solo necesitas crear una funcion de agregacion propia
que concatene los textos que le entregues separandolos con un espacio.
No es ciencia de cohetes:

create or replace function concat(text, text) returns text
called on null input language plpgsql immutable
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 aggregate text_concat (text) (sfunc = concat, stype = text);

Luego necesitas una consulta que obtenga todos los datos que te hacen
falta:

select a.idficha, nomfamil, nomdescr, porcentaje || ' ' || desc_hilo
from ficha_tejeduria a join
mfamilias b on (a.idfamil = b.idfamil) join
ficha_hilados c on (a.idficha = c.idficha) join
mdescripcion d on (a.iddescr = d.iddescr) join
mhilos e on (c.idhilo = e.idhilo)
where a.idficha = 125;

idficha | nomfamil | nomdescr | ?column?
---------+----------+----------+-----------------
125 | JERSEY | SIMPLE | 20X COC
125 | JERSEY | SIMPLE | 30X COC
125 | JERSEY | SIMPLE | 50X ALGOD. OPEN
(3 lignes)

Luego lo juntas todo con la funcion de agregacion que acabamos de crear:

select a.idficha, nomfamil, nomdescr, text_concat(porcentaje || ' ' || desc_hilo)
from ficha_tejeduria a join
mfamilias b on (a.idfamil = b.idfamil) join
ficha_hilados c on (a.idficha = c.idficha) join
mdescripcion d on (a.iddescr = d.iddescr) join
mhilos e on (c.idhilo = e.idhilo)
where a.idficha = 125
group by a.idficha, nomfamil, nomdescr;

idficha | nomfamil | nomdescr | text_concat
---------+----------+----------+---------------------------------
125 | JERSEY | SIMPLE | 20X COC 30X COC 50X ALGOD. OPEN
(1 ligne)

Si lo quieres en una vista, es facil:

create view tejidos as
select a.idficha, concat(concat(nomfamil, nomdescr), text_concat(porcentaje || ' ' || desc_hilo))
from ficha_tejeduria a join
mfamilias b on (a.idfamil = b.idfamil) join
ficha_hilados c on (a.idficha = c.idficha) join
mdescripcion d on (a.iddescr = d.iddescr) join
mhilos e on (c.idhilo = e.idhilo)
where a.idficha = 125
group by a.idficha, nomfamil, nomdescr;

select * from tejidos;

idficha | concat
---------+-----------------------------------------------
125 | JERSEY SIMPLE 20X COC 50X ALGOD. OPEN 30X COC
(1 ligne)

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message César Piñera García 2008-03-27 23:52:38 RE: acceso mediante zeos
Previous Message Mauro A. Morales M. 2008-03-27 23:24:20 Re: QUE INTERFAZ GRAFICA(HERRAMIENTA) DEBO UTILIZAR PARA MANIPULAR DATOS POSTGRESQL