Ver Mensaje Individual
  #2 (permalink)  
Antiguo 11/08/2008, 08:58
yanqui60
 
Fecha de Ingreso: agosto-2008
Mensajes: 1
Antigüedad: 15 años, 8 meses
Puntos: 0
Respuesta: Cómo poner nombres a consultas para el uso de joins

Aqui esta un ejemplo que podria ayudarte:

select p.appellido, p.nombre, t1.codigo_o_valor telefono, t2.codigo_o_valor celular, t3.codigo_o_valor email
from hhyde.persona p
join hhyde.telecom t1 on t1.persona_id = p.persona_id and t1.telecomtipo_id = 1
join hhyde.telecom t2 on t2.persona_id = p.persona_id and t2.telecomtipo_id = 2
join hhyde.telecom t3 on t3.persona_id = p.persona_id and t3.telecomtipo_id = 3
;

Aqui esta el codigo DDL para crear las tablas del ejemplo y de rellenar algunos datos:

create table hhyde.persona ( persona_id number not null
, appellido varchar2(20) not null
, nombre varchar2(20) not null
);
alter table hhyde.persona add constraint persona_pk primary key (persona_id);

create table hhyde.telecom_tipo ( telecomtipo_id number not null
, nombre varchar2(20) not null
, descripcion varchar2(4000)
);
alter table hhyde.telecom_tipo add constraint tlcmtipo_pk primary key (telecomtipo_id);
alter table hhyde.telecom_tipo add constraint tlcmtipo_uk unique (nombre);

create table hhyde.telecom ( telecom_id number not null
, telecomtipo_id number not null
, persona_id number not null
, codigo_o_valor varchar2 (1000) not null
);
alter table hhyde.telecom add constraint telecom_pk primary key (telecom_id);
alter table hhyde.telecom add constraint telecom_uk unique (persona_id, telecomtipo_id);
alter table hhyde.telecom add constraint telecom_tlcmtipo_fk foreign key (telecomtipo_id) references telecom_tipo (telecomtipo_id);

alter table hhyde.telecom add constraint telecom_persona_fk foreign key (persona_id) references persona (persona_id);


insert into hhyde.telecom_tipo (telecomtipo_id, nombre, descripcion) values (1, 'Telefono', 'Telefono de casa');
insert into hhyde.telecom_tipo (telecomtipo_id, nombre, descripcion) values (2, 'Celular', 'Telefono celular');
insert into hhyde.telecom_tipo (telecomtipo_id, nombre, descripcion) values (3, 'EMail', 'Correo Electronico');

insert into hhyde.persona (persona_id, appellido, nombre) values (1, 'De Soto', 'Jesus');
insert into hhyde.persona (persona_id, appellido, nombre) values (2, 'Calderon', 'Felipe');
insert into hhyde.persona (persona_id, appellido, nombre) values (3, 'Uribe', 'Alvaro');

insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (1, 1, 1, '22-33-44-55' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (2, 2, 1, '88-54-22-89' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (3, 3, 1, '[email protected]' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (4, 1, 2, '754-765-47-54' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (5, 2, 2, '4467-2455-3' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (6, 3, 2, '[email protected]' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (7, 1, 3, '87473-58-592' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (8, 2, 3, '5521-27-726-23' );
insert into hhyde.telecom (telecom_id, telecomtipo_id, persona_id, codigo_o_valor) values (9, 3, 3, '[email protected]' );
commit;

A tus ordenes,

Yanqui60