問題

yukaさんの回答

fumikoの回答

考え方

  • ①phone_typeがhomである行の集合②phone_typeがfaxである行の集合を作り
①②を全外部結合→③
  • ③とpersonnelをemp_idで結合

SQL

 select personnel.first_name,personnel.last_name,home,fax
 from personnel left outer join 
 (select(case when h.emp_id is not null then h.emp_id else f.emp_id end)as emp_id2
             ,h.phone_nbr as home,f.phone_nbr as fax
       from (select * from phones where phone_type = 'hom') as h
             full outer join
            (select * from phones where phone_type = 'fax') as f
             on h.emp_id = f.emp_id)as ph2 
  on ph2.emp_id2 = personnel.emp_id

結果

       first_name    |    last_name     |     home     |     fax
 --------------------+------------------+--------------+-----------
 山田                | 太郎             | 1111         | 2222
 上野                | 二郎             | 3333         |
 高田                | 三郎             |              | 4444
 松岡                | 四郎             |              |
最終更新:2008年06月05日 17:44