我试图向每位员工展示SC,DC,DV,每个人都可以有1个或更多选择,例如eng 1可以有sc和dc。
所以需要得到
但是得到
我用case来设置列和数据,但是无法在同一行上获取数据
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 :(得分:2)
在这些列上使用max,试试这个
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, ;