0

我正在尝试计算所有空缺职位,“职位”表包含所有可用职位,“员工”表包含该职位的所有员工。当我运行下面的语句时,我收到

Department     Job Code     Count(*)

10             2            1

10             1            1

但我期待这个;因为目前职位代码“3”中没有员工

Department     Job Code     Count(*)

10             3            0

10             2            1

10             1            1

如何让 Job Code 3 的计数显示为“0”或“null”也是可以接受的

select pos.department, pos.job_code, count(*)
from paposition pos
left join employee emp
on (pos.department = emp.department
and pos.job_code = emp.job_code)
where pos.job_code in ('1', '2', '3')
and pos.end_date = '01-Jan-1700'
and (emp.emp_status like 'A%'
or emp.emp_status like 'L%')
and emp.department = '0010'
group by pos.department, pos.job_code
order by pos.department, pos.job_code
4

1 回答 1

3

问题是您通过在子句中left outer join包含列来撤消 。empwhere

您需要将条件移至on子句:

select pos.department, pos.job_code, count(*)
from paposition pos left join
     employee emp
     on (pos.department = emp.department and
        pos.job_code = emp.job_code and
        (emp.emp_status like 'A%' or emp.emp_status like 'L%') and emp.department = '0010'
where pos.job_code in ('1', '2', '3') and
      pos.end_date = '01-Jan-1700' and
group by pos.department, pos.job_code
order by pos.department, pos.job_code;

(或者,您也可以在 for和子句中检查是否为NULL有效值。)whereemp.emp_statusemp.department

于 2013-07-17T20:49:54.977 回答