每个员工的左加入重复

时间:2018-11-27 20:44:42

标签: sql oracle oracle11g left-join

我正在使用Oracle 11g和SQL Developer 16.260。

表1-员工

ID  FIRST_NAME  LAST_NAME
1   CHRIS       PETERS
2   MICHELLE    ANDREWS
3   ANDY        JONES

表2 –技能

ID  SKILL
1   WRITING
2   READING
3   COMPUTERS
4   EXCEL
5   TYPING 
6   MS WORD

表3 – EMPLOYEE_SKILLS

ID  EMPLOYEE_ID SKILLS_ID
1   1           1
2   1           2
3   1           4
4   2           1
5   2           2
6   2           3
7   2           6
8   3           1
9   3           6
10  3           5

我正在寻找可以给我以下结果的查询

EMPLOYEE.FIRST_NAME EMPLOYEE.LAST_NAME  SKILL      COMPLETE
CHRIS               PETERS              WRITING    Complete
CHRIS               PETERS              READING    Complete
CHRIS               PETERS              COMPUTERS  Incomplete
CHRIS               PETERS              EXCEL      Complete
CHRIS               PETERS              TYPING     Incomplete
CHRIS               PETERS              MS WORD    Incomplete
MICHELLE            ANDREWS             WRITING    Complete
MICHELLE            ANDREWS             READING    Complete
MICHELLE            ANDREWS             COMPUTERS  Complete
MICHELLE            ANDREWS             EXCEL      Incomplete
MICHELLE            ANDREWS             TYPING     Incomplete
MICHELLE            ANDREWS             MS WORD    Complete
ANDY                JONES               WRITING    Complete
ANDY                JONES               READING    Incomplete
ANDY                JONES               COMPUTERS  Incomplete
ANDY                JONES               EXCEL      Incomplete
ANDY                JONES               TYPING     Complete
ANDY                JONES               MS WORD    Complete

我已经尝试了

的其他变体
SELECT E.FIRST_NAME, E.LAST_NAME, S.SKILL, CASE ES.SKILLS_ID IS NULL THEN        'Incomplete' ELSE 'COMPLETE' END
FROM EMPLOYEE E, EMPLOYEE_SKILLS ES
LEFT JOIN SKILLS S ON S.ID = ES.SKILLS_ID
WHERE EMPLOYEE.ID=EMPLOYEE_SKILLS.EMPLOYEE_ID

我似乎无法让每位员工重复技能。

1 个答案:

答案 0 :(得分:1)

使用cross join生成行,使用left join引入值:

SELECT E.FIRST_NAME, E.LAST_NAME, S.SKILL,
       (CASE ES.SKILLS_ID IS NULL THEN 'Incomplete' ELSE 'Complete' END)
FROM EMPLOYEE E CROSS JOIN
     SKILLS S LEFT JOIN
     EMPLOYEE_SKILLS ES 
     ON S.ID = ES.SKILLS_ID AND E.ID = ES.EMPLOYEE_ID;