2015-02-10 105 views
1

我試圖顯示SC,DC,DV對每個員工的工作人員,每個人都可以有一個或多個選擇,例如eng 1可以有sc和dc。Oracle/SQL多個真實案例陳述

所以需要得到

enter image description here

我使用情況來設置列和數據獲取,但不能得到在同一行的數據

SELECT DISTINCT e.ENG_ID, 
      e.NAME, 
      e.SURNAME, 
      e.POST_CODE, 
      CASE 
       WHEN C.CLEARANCE_ID = 1 THEN 'YES' 
       ELSE 'NO' 
      END AS SC, 
      CASE 
       WHEN C.CLEARANCE_ID = 2 THEN 'YES' 
       ELSE 'NO' 
      END AS BS, 
      CASE 
       WHEN C.CLEARANCE_ID = 3 THEN 'YES' 
       ELSE 'NO' 
      END AS DV 
FROM MAN_ENGINEERS e 
    left join MAN_CLEARANCE c 
      ON e.ENG_ID = c.ENG_ID; 

希望這樣做在這些列感

+0

你可能有每個工程的多個間隙,所以你要哪一個? – Mihai 2015-02-10 22:16:41

回答

2

利用最高,試試這個

SELECT  e.ENG_ID, 
      e.NAME, 
      e.SURNAME, 
      e.POST_CODE, 
      MAX 
      (
       CASE 
       WHEN C.CLEARANCE_ID = 1 THEN 'YES' 
       ELSE 'NO' 
       END 
      ) AS SC, 
      MAX 
      (
       CASE 
       WHEN C.CLEARANCE_ID = 2 THEN 'YES' 
       ELSE 'NO' 
       END 
      ) AS BS, 
      MAX 
      (
       CASE 
       WHEN C.CLEARANCE_ID = 3 THEN 'YES' 
       ELSE 'NO' 
       END 
      ) AS DV 
FROM MAN_ENGINEERS e 
    left join MAN_CLEARANCE c 
      ON e.ENG_ID = c.ENG_ID 
GROUP BY e.ENG_ID, 
      e.NAME, 
      e.SURNAME, 
      e.POST_CODE, ; 
+0

完全完全忘了那個。非常感謝 – 2015-02-10 23:02:33