我该如何将其转换为左外连接?

时间:2018-01-17 09:51:06

标签: oracle oracle11g oracle10g

我有一个正常运行的代码,我想要的是将其转换为左外连接。这是代码:

SELECT 
  dept_code "Dept Code", 
  INITCAP(dept_name) "Dept Name", 
  (SELECT COUNT(*)
    FROM EMPLOYEES
    WHERE 
        dept_dept_code = DEPARTMENTS.DEPT_CODE
        AND
        resign_date IS NULL
   ) "Emp Count"
FROM 
   DEPARTMENTS;

我试过了,但输出结果不一样:

  SELECT DISTINCT
         dept.dept_code "Dept Code",
         INITCAP (dept.dept_name) "Dept Name",
         COUNT (empl.dept_dept_code) "Emp Count"
    FROM DEPARTMENTS dept
         LEFT OUTER JOIN EMPLOYEES empl
            ON (dept.DEPT_CODE = empl.dept_dept_code
                AND empl.resign_date IS NOT NULL)
GROUP BY dept.dept_code, dept.dept_name

1 个答案:

答案 0 :(得分:1)

也许是这样的事情?

  SELECT d.dept_code "Dept Code",
         INITCAP (d.dept_name) "Dept Name",
         COUNT (e.dept_dept_code) "Emp Count"
    FROM SS_DEPARTMENTS d
         LEFT OUTER JOIN ss_empliyees e
            ON e.dept_dept_code = d.dept_code AND e.resign_date IS NULL
GROUP BY d.dept_code, INITCAP (d.dept_name);