1

数据库中的总 EmpId = 74。Databse 中 2013 年 9 月的活动天数 = 22 我想在 MIS_Opus 表中隔离员工未填写生产的日期。

由于 FULL OUTER Join 不起作用,我正在使用 except 查询。通过这个查询,我可以通过 c# 函数传递 empid 来获取每个员工的未填写日期。因为该循环将执行 74 次 SQL 并返回 C#。请让我知道如何在 SQL 本身中一次性获取所有员工的未填写日期。我正在使用 SQL 2012 和 VS 2012 c#。

谢谢,

select _Date from MIS_BM_Calendar c
where c.Month = 'September 2013'  and c.DayShiftStatus = 'active'
except
select _Date from MIS_Opus o
where o.EmpId=@Empid
4

1 回答 1

1

One way is to build a list of all employee + day combinations using a cross join. Then you can use a left join to check if there is an employee entry for that day.

select  days._Date as TheDay
,       emps.EmpId as EmployeeWithMissingEntry
from    (
        select  distinct _Date
        from    MIS_BM_Calendar
        where   Month = 'September 2013'
                and DayShiftStatus = 'active'
        ) days
cross join -- One row for each combination of employee and date
        (
        select  distinct EmpId
        from    MIS_Opus
        ) emps
left join
        MIS_Opus o
on      o._Date = days._Date
        and o.EmpId = emps.EmpId
where   o._Date is null -- Employee entry for day not found
于 2013-10-22T08:21:46.753 回答