Oracle
 sql >> Database >  >> RDS >> Oracle

Oracle SELECT QUERY per una relazione da uno a molti tra due tabelle

Un metodo utilizza l'aggregazione condizionale:

select t1.PERSON_ID, t1.FIRST_NAME, t1.MIDDLE_NAME, t1.LAST_NAME, 
       max(case when t2.phone_type = 'BUSINESS' then t2.PHONE_NUMBER end) as business,
       max(case when t2.phone_type = 'PERSONAL' then t2.PHONE_NUMBER end) as personal,
       max(case when t2.phone_type = 'HOME' then t2.PHONE_NUMBER end) as home
from Table1 t1 inner join
     Table2 t2
     on t2.PERSON_ID = t1.PERSON_ID
where t2.PHONE_TYPE in ('BUSINESS', 'PERSONAL', 'HOME') 
group by t1.PERSON_ID, t1.FIRST_NAME, t1.MIDDLE_NAME, t1.LAST_NAME;