表中的加班专栏

时间:2014-01-26 16:34:34

标签: sql sql-server sql-server-2008 tsql sql-server-2012

办公时间:上午10:00至下午18:00。

如果有人工作时间超过'8小时',我已写过这个查询以显示加班费,例如如果他工作'08:44小时'比加班显示00:44,但现在我不想要这个。

我不想在TOTAL WORK TIME的基础上计算它。我希望如果有人工作时间超过时钟时间18:00 pm,那么应该显示加班费。

e.g。如果有人工作时间从上午10:50到下午18:33,加班专栏应该显示00:33。只有当一个人在18:00以后工作时,才应加班加点。

n.b。实际上,我们的办公室政策是支付员工加班津贴,所以他们只计算加班时间超过18:00时钟,即使有人在下午17:00到达办公室并且工作到下午18:54,即便如此他也会加班支付54分钟,除此之外,他没有工作的时间会导致工资教育,但仍然适用于加班津贴。

WITH Times AS
(   SELECT  emp.EmplID, 
            emp.EmplName,
            InTime = MIN(atd.RecTime),
            OutTime = MAX(atd.RecTime),
            TimeWorked = DATEDIFF(MINUTE, MIN(atd.RecTime), MAX(atd.RecTime)),
            OverTime = DATEDIFF(MINUTE, MIN(atd.RecTime), MAX(atd.RecTime)) - 480,
            [DateVisited] = atd.RecDate
    FROM    AtdRecord atd 
            INNER JOIN HrEmployee emp 
                ON atd.EmplID = emp.EmplID 
    GROUP BY emp.EmplID, emp.EmplName, atd.RecDate
    HAVING COUNT(atd.RecTime) > 1
)
SELECT  t.EmplID,
        t.EmplName,
        t.InTime,
        t.OutTime,
        t.DateVisited,    
        TimeWorked = CONVERT(CHAR(5), DATEADD(MINUTE, t.TimeWorked, 0), 8),
        OverTime = CASE WHEN t.OverTime < 0 THEN '-' ELSE '' END +
                                CONVERT(CHAR(5), DATEADD(MINUTE, ABS(t.OverTime), 0), 8)
FROM Times t

1 个答案:

答案 0 :(得分:0)

这是你想要的逻辑吗?

DECLARE @EndTimeA   TIME = '18:00',
        @EndTimeB   TIME = '18:05',
        @EndTimeC   TIME = '19:00',
        @EndTimeD   TIME = '17:55'


SELECT   OvertimeMM_A   =   CASE    WHEN @EndTimeA > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeA)
                                ELSE 0
                            END
        ,OvertimeMM_B   =   CASE    WHEN @EndTimeB > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeB)
                                ELSE 0
                            END
        ,OvertimeMM_C   =   CASE    WHEN @EndTimeC > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeC)
                                ELSE 0
                            END
        ,OvertimeMM_D   =   CASE    WHEN @EndTimeD > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeD)
                                ELSE 0
                            END

表示为TIME;

DECLARE @EndTimeA   TIME = '18:00',
        @EndTimeB   TIME = '18:05',
        @EndTimeC   TIME = '19:00',
        @EndTimeD   TIME = '17:55'


SELECT   CONVERT(CHAR(5), DATEADD(MINUTE, OvertimeMM_A, '00:00'), 8)
        ,CONVERT(CHAR(5), DATEADD(MINUTE, OvertimeMM_B, '00:00'), 8)
        ,CONVERT(CHAR(5), DATEADD(MINUTE, OvertimeMM_C, '00:00'), 8)
        ,CONVERT(CHAR(5), DATEADD(MINUTE, OvertimeMM_D, '00:00'), 8)
FROM 
(
    SELECT   OvertimeMM_A   =   CASE    WHEN @EndTimeA > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeA)
                                    ELSE 0
                                END
            ,OvertimeMM_B   =   CASE    WHEN @EndTimeB > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeB)
                                    ELSE 0
                                END
            ,OvertimeMM_C   =   CASE    WHEN @EndTimeC > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeC)
                                    ELSE 0
                                END
            ,OvertimeMM_D   =   CASE    WHEN @EndTimeD > '18:00' THEN DATEDIFF(MINUTE, '18:00', @EndTimeD)
                                    ELSE 0
                                END
)DT