我写了这个查询,以显示特定日期(如果他来了)员工的总工作时间和加班时间。我想如果对于特定日期,人的INtime和OutTime是空的,那么将00:00放入他的intime,outtime,totalworkingtime,overtime,例如它是星期天所以很明显没有InTime和OutTime然后它应该在时间列中显示00:00。 注意:仅在提及人InTIme时输入日期,否则不输入DateVisited。
e.g。
EmplID EmplName ShiftID intime Outtime totalworking overtime dateVisited
0000001 John S001 00:00 00:00 00:00: 00:00 2013-12-01
查询:
with times as (
SELECT t1.EmplID
, t3.EmplName
, min(t1.RecTime) AS InTime
, max(t2.RecTime) AS [TimeOut]
, t4.ShiftId as ShiftID
, t4.StAtdTime as ShStartTime
, t4.EndAtdTime as ShEndTime
, cast(min(t1.RecTime) as datetime) AS InTimeSub
, cast(max(t2.RecTime) as datetime) AS TimeOutSub
, t1.RecDate AS [DateVisited]
FROM AtdRecord t1
INNER JOIN
AtdRecord t2
ON t1.EmplID = t2.EmplID
AND t1.RecDate = t2.RecDate
AND t1.RecTime < t2.RecTime
inner join
HrEmployee t3
ON t3.EmplID = t1.EmplID
inner join AtdShiftSect t4
ON t3.ShiftId = t4.ShiftId
group by
t1.EmplID
, t3.EmplName
, t1.RecDate
, t4.ShiftId
, t4.StAtdTime
, t4.EndAtdTime
)
SELECT
EmplID
,EmplName
,ShiftId As ShiftID
,InTime
,[TimeOut]
,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) TotalWorkingTime
,[DateVisited]
,CASE WHEN [InTime] IS NOT NULL AND [TimeOut] IS NOT NULL THEN
CONVERT(char(5),CASE WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S002' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME),CAST([TimeOutSub] AS DATETIME)),0), 108),5)
WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S001' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME), CAST([TimeOutSub] AS DATETIME)),0), 108),5)
ELSE '00:00' END, 108)
ELSE 'ABSENT' END AS OverTime
FROM times order by EmplID, ShiftID, DateVisited
答案 0 :(得分:1)
如果您的问题完全取决于知道某个日期是工作日/假日/周末,我建议使用DataWarehouse解决方案。创建一个包含所有假期/周末的表,并检查它。
根据日历和您正在使用的其他日期,有许多脚本可供您创建,但最简单的方法是在接下来的10年左右计算所有假期时间。例如圣诞节总是在12月23日,感恩节总是在11月3周我会创建一个这样的表
CREATE TABLE Holiday
(
HolidayID INT NOT NULL, --surrogate key, but you can just as easily make HolidayDate the Natural key
HolidayDate DATE NOT NULL
HolidayName NVARCHAR(30),--encase you need non English holiday names saved
...
...
..
INSERT INTO Holiday (HolidayDate,HolidayName) VALUES (2014-12-23, 'Christmas')
INSERT INTO Holiday (HolidayDate,HolidayName) VALUES (2015-12-23, 'Christmas')
INSERT INTO Holiday (HolidayDate,HolidayName) VALUES (2016-12-23, 'Christmas')
INSERT INTO Holiday (HolidayDate,HolidayName) VALUES (2017-12-23, 'Christmas')
.....
.....
....
或者如果你想得到更深入的深度,你可以在表格(日期维度表)中映射出未来10年的任何一天。然后将那些日子标记为假期,取消工作,销售季度以及什么不是。 (谷歌日期维度表)