考勤数据生成过程

CREATE PROCEDURE OutInCalc2 @dDate0 datetime, @dDate1 datetime
AS
/*清除历史数据*/
DELETE Kq_OutIn WHERE (OutTime BETWEEN @dDate0 AND @dDate1) or (InTime BETWEEN @dDate0 AND @dDate1)
/*获取原始数据*/
SELECT ID, EmpID, FDateTime, FType
  INTO #tSource
    FROM Kq_Source
    WHERE 1=2
TRUNCATE TABLE #tSource
INSERT INTO #tSource (EmpID, FDateTime, FType)
  SELECT EmpID, FDateTime, FType
    FROM Kq_Source
    WHERE (FDatetime BETWEEN @dDate0 AND @dDate1)
       AND FType in (4,5) AND EmpID IS NOT NULL
    ORDER BY EmpID,FDatetime
/*清除重复打卡*/
DECLARE @KqDkJgsj int
SELECT @KqDkJgsj =KqDkJgsj FROM S_SysSet WHERE ID=1
DELETE #tSource WHERE ID IN
  (SELECT A.ID FROM #tSource A INNER JOIN #tSource B
    ON A.ID = B.ID +1 AND DateDiff(minute, B.FDatetime,  A.FDatetime) <= @KqDkJgsj AND A.EmpID = B.EmpID )
/*写结果数据*/
INSERT INTO Kq_OutIn (EmpID, OutTime, InTime, WaiChuSj)
  SELECT EmpID =COALESCE(O.EmpID,I.EmpID),
       O.FDatetime, I.FDatetime, DateDiff(minute,O.FDatetime,I.FDatetime)
    FROM (SELECT * FROM #tSource WHERE FType=4) AS O FULL JOIN (SELECT * FROM #tSource WHERE FType=5) AS I
     ON O.EmpID=I.EmpID AND I.ID=O.ID+1
    ORDER BY COALESCE(O.ID,I.ID)
DROP TABLE #tSource
GO

你可能感兴趣的:(职场,休闲,考勤数据生成过程)