我有两张桌子:1。EMP(持有员工信息)和2.活动(包含empid,status:working,leave)。 状态可能重复,因此我可以在同一日期重复相同的状态。
我希望我的输出位于以下列中:
emp absence working
---- ------- --------
我的查询如下:
select id ,employee, count(tr)as [working] , count(om) as [Absence]
from(
( select emp_id as id, rep as employee from emp) as [pc101]
left outer join
(
select distinct emp_id, mydate as [tr]
from activities
where status = "Working"
) as [pc1]
on pc1.emp_id = pc101.id
)
left outer join
(
select distinct emp_id, mydate as [om]
from activities
where status = "absent"
) as pc2
on pc101.id = pc2.emp_id
group by employee, id
我使用了左外连接来确保即使是符合条件的员工也会出现。 问题在于,当员工具有这两种状态时,工作和工作的价值总和。不存在。