我有3张桌子:
表员工:
EmpID | EmpName
1 |'Mr. A'
2 |'Mr. B'
Table OffDays:
OffDayID | OffDayName | OffDayValue
1 | 'Saturday' | 7
2 | 'Sunday' | 1
表出席率:
AttendanceID | Date | EmpID
1 | '2014-06-02' | 1
2 | '2014-06-03' | 1
3 | '2014-06-04' | 1
4 | '2014-06-05' | 1
5 | '2014-06-10' | 1
员工'先生'2014-06-06'周五和'2014-06-09'星期一没有'身份证1'。 如何正确查询以显示假期'0214-06-07',周六和'2014-06-8'周日之间缺席。
请帮忙。
我的存储过程是
DECLARE @DATE_FROM AS DATETIME
DECLARE @DATE_TO AS DATETIME
SET @DATE_FROM = '2014-05-01'
SET @DATE_TO = '2014-05-31'
DECLARE @DATE_FROM2 AS DATETIME
DECLARE @DATE_TO2 AS DATETIME
SET @DATE_FROM2 = @DATE_FROM
SET @DATE_TO2 = @DATE_TO
TRUNCATE TABLE AttendanceRegister
--------------------Present,Absent,Leave-----------------------
WHILE @DATE_FROM <= @DATE_TO
BEGIN
INSERT INTO AttendanceRegister
SELECT E.EmployeeID,
@DATE_FROM,
CASE
WHEN A.AttendanceID IS NULL OR A.TimeIn IS NULL
THEN
CASE
WHEN @DATE_FROM IN
(
SELECT CONVERT(DATE,LeaveDate)
FROM LeaveDetail
WHERE EmployeeID = E.EmployeeID
)
THEN 'L'
ELSE 'A'
END
ELSE 'P'
END
FROM Employee E
LEFT OUTER JOIN Attendance A ON A.[DayofMonth] = @DATE_FROM
AND A.EmployeeID = E.EmployeeID AND A.IS_DELETED = 0
WHERE E.IS_DELETED = 0 AND E.IS_ACTIVE = 1
AND DATEPART(WEEKDAY,@DATE_FROM)
NOT IN (
SELECT OffDayValue
FROM OffDays
)
SET @DATE_FROM = DATEADD(D,1,@DATE_FROM)
END
-------------------------Holidays------------------------------
WHILE @DATE_FROM2 <= @DATE_TO2
BEGIN
IF DATEPART(WEEKDAY,@DATE_FROM2)
IN (
SELECT OffDayValue
FROM OffDays
)
BEGIN
INSERT INTO AttendanceRegister
SELECT E.EmployeeID, @DATE_FROM2,'H'
FROM Employee E
WHERE E.IS_DELETED = 0 AND E.IS_ACTIVE = 1
END
SET @DATE_FROM2 = DATEADD(D,1,@DATE_FROM2)
END
---------------------------------------------------------------
SELECT *
FROM AttendanceRegister