我是否知道如何将表合并中的日期与GETDATE()
结合以显示结果?
对于示例,
SELECT [FORM_NO]
, [FORM_KIND]
, [EMP_NO]
, [ORG_ID]
, [LEAVE_NO]
, [SHIFT_NO]
, [START_DAY] --// start date apply leave
, [start_time]
, [END_DAY]
, [end_time] --// end date apply leave
, [SUMHOURS]
, [SUM_DAYS]
, [ATTORNEY_EMP_NO]
, [APPLY_REASON]
, [NOTE]
, [status]
, [FILL_EMP_NO]
, [FILL_DATE]
FROM [LEAVETb]
从这张表中我有开始日期和结束日期。接下来,我想加入EMP_Info
,EMP_Shift
,EMP_Desc
和EMP_HistScan
表。
SELECT EMPINFO.EP_EMP_DEPT
, EMPINFO.EP_EMP_SECTION
, EMPINFO.EP_EMP_ID
, EMPINFO.EP_EMP_NAME
, EMPINFO.EP_EMP_SHIFT
, SHIFTCAL.EP_SHIFT
FROM [EP_EMP_INFO] EMPINFO -- All EMP Details Table
JOIN [EP_SHIFT_CALENDAR] SHIFTCAL -- ALL SHIFT Type table
ON SHIFTCAL.EP_SHIFT_NAME = EMPINFO.EP_EMP_SHIFT
AND SHIFTCAL.EP_SHIFT_DATE = CONVERT(VARCHAR(8), GETDATE(), 112)
LEFT JOIN
(
SELECT CONVERT(VARCHAR(8), STUFF(STUFF(FIRSTSCAN.EP_SCAN_DATE, 12, 0, ':'), 15, 0, ':'), 112) AS SCANDATE
, FIRSTSCAN.EP_EMP_COMPANY
, FIRSTSCAN.EP_EMP_ID
, FIRSTSCAN.EP_EMP_NAME
, FIRSTSCAN.EP_EMP_DEPT
, FIRSTSCAN.EP_EMP_SECTION
, FIRSTSCAN.EP_EMP_SHIFT
, FIRSTSCAN.EP_SHIFT
, FIRSTSCAN.EP_SCAN_DATE
, FIRSTSCAN.LATEIN
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY SCANHIST.EP_EMP_ID, CASE
WHEN RIGHT(SCANHIST.EP_SCAN_DATE, 6) < '130000' AND SHIFTCAL.EP_SHIFT = 'N1' THEN CONVERT(VARCHAR(8), DATEADD(DAY, -1, CONVERT(VARCHAR(8), LEFT(SCANHIST.EP_SCAN_DATE, 8), 112)), 112) ELSE LEFT(SCANHIST.EP_SCAN_DATE, 8)
END ORDER BY SCANHIST.EP_EMP_ID, SCANHIST.EP_SCAN_DATE) AS RowNum
, SCANHIST.EP_SCAN_DATE
, CASE
WHEN RIGHT(SCANHIST.EP_SCAN_DATE, 6) < '130000' AND SHIFTCAL.EP_SHIFT = 'N1' THEN CONVERT(VARCHAR(8), DATEADD(DAY, -1, CONVERT(VARCHAR(8), LEFT(SCANHIST.EP_SCAN_DATE, 8), 112)), 112) ELSE LEFT(SCANHIST.EP_SCAN_DATE, 8)
END AS EMP_WORKDATE
, EMPINFO.EP_EMP_COMPANY
, SCANHIST.EP_EMP_ID
, SCANHIST.EP_EMP_NAME
, SCANHIST.EP_EMP_DEPT
, SCANHIST.EP_EMP_SECTION
, SCANHIST.EP_EMP_SHIFT
, SHIFTCAL.EP_SHIFT
, CASE WHEN RIGHT(SCANHIST.EP_SCAN_DATE, 6) > REPLACE(SHIFTDESC.EP_SHIFT_TIMEFR, ':', '') THEN 1 END AS LATEIN
FROM [EP_SCAN_HIST] SCANHIST --// Employee Scan IN / Out time Table
JOIN [EP_EMP_INFO] EMPINFO ON EMPINFO.EP_EMP_ID = SCANHIST.EP_EMP_ID
JOIN [EP_SHIFT_CALENDAR] SHIFTCAL
ON SHIFTCAL.EP_SHIFT_NAME = SCANHIST.EP_EMP_SHIFT
AND SHIFTCAL.EP_SHIFT_DATE = LEFT(SCANHIST.EP_SCAN_DATE,8)
JOIN [EP_SHIFT_DESC] SHIFTDESCON SHIFTDESC.EP_SHIFT_NAME = SHIFTCAL.EP_SHIFT
WHERE 1=1
AND SCANHIST.EP_SCAN_DATE >= CONVERT(VARCHAR(8),GETDATE(),112) + ' ' + CASE WHEN (SHIFTCAL.EP_SHIFT <> 'R1' AND SHIFTCAL.EP_SHIFT <> 'R2') THEN REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,-4,SHIFTDESC.EP_SHIFT_TIMEFR + ':00'),108),':','') ELSE REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,-0,SHIFTDESC.EP_SHIFT_TIMEFR + ':00'),108),':','') END
AND SCANHIST.EP_SCAN_DATE < CASE WHEN (SHIFTCAL.EP_SHIFT = 'N1') THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,CONVERT(VARCHAR(8),GETDATE(),112)),112) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') ELSE CASE WHEN (SHIFTCAL.EP_SHIFT = 'R1' OR SHIFTCAL.EP_SHIFT = 'R2') THEN CONVERT(VARCHAR(8),GETDATE(),112) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') ELSE CONVERT(VARCHAR(8),GETDATE(),112) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END END
AND SCANHIST.EP_TRANS_LOC = 'IN'
AND EMPINFO.EP_EMP_LEVEL > '10'
AND EMPINFO.EP_EMP_LEVEL <> ''
AND (SHIFTCAL.EP_SHIFT = 'D1'
OR SHIFTCAL.EP_SHIFT = 'NS')
) FIRSTSCAN
WHERE 1=1 AND FIRSTSCAN.RowNum = 1
)FIRSTSCAN ON FIRSTSCAN.EP_EMP_ID = EMPINFO.EP_EMP_ID
WHERE 1=1
AND EMPINFO.EP_EMP_RESIGN <> 'Y'
AND EMPINFO.EP_EMP_DOJ <= CONVERT(VARCHAR(8),GETDATE(),112)
AND EMPINFO.EP_EMP_LEVEL > '10'
AND EMPINFO.EP_EMP_LEVEL <> ''
AND (SHIFTCAL.EP_SHIFT = 'D1' OR SHIFTCAL.EP_SHIFT = 'NS')
AND FIRSTSCAN.EP_EMP_ID IS NULL --// when detect the EMP_ID scan is null , the result will show.
ORDER BY EMPINFO.EP_EMP_DEPT,EMPINFO.EP_EMP_SECTION,EMPINFO.EP_EMP_TYPE,EMPINFO.EP_EMP_ID
Emp ScanIN / OUT时间将在SQL中捕获GetDate()
,所以现在如何合并开始日期和时间当IS NULL(缺席或onleave)时,FIRSTSCAN.EP_EMP_ID
的结束日期,以便显示正确的结果?