新列计算加班时间

时间:2014-01-22 10:07:04

标签: sql tsql sql-server-2012

我编写并管理此查询以计算一个人每天通过dateDiff函数工作的总时间现在我被困在一个地方。我想计算一个人是否已经做了一段时间而不是一个新列应该以hh:mm显示OVERITME。

我办公室的总工作时间是08:00,超过8小时被认为是加班时间,例如如果一个人工作时间超过08:35,则表明某人已经工作了00:35

QUERY:

with times as (
SELECT    t1.EmplID
        , t3.EmplName
        , min(t1.RecTime) AS InTime
        , max(t2.RecTime) AS [TimeOut]
        , 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 
group by 
          t1.EmplID
        , t3.EmplName
        , t1.RecDate
)
SELECT EmplID
                ,EmplName
                ,InTime
                ,[TimeOut]
                ,[DateVisited]
                ,CASE 
                    WHEN minpart = 0
                        THEN CAST(hourpart AS NVARCHAR(200)) + ':00'
                    WHEN minpart <10
                        THEN CAST(hourpart AS NVARCHAR(200)) + ':0'+ CAST(minpart AS NVARCHAR(200))
                    ELSE CAST(hourpart AS NVARCHAR(200)) + ':' + CAST(minpart AS NVARCHAR(200))

END AS 'total time'
            FROM (
                SELECT EmplID
                    ,EmplName
                    ,InTime
                    ,[TimeOut]
                    ,[DateVisited]
                    ,DATEDIFF(minute, InTime, [TimeOut])/60 AS hourpart
                    ,DATEDIFF(minute, InTime, [TimeOut]) % 60 AS minpart
                FROM times
                ) source

输出: enter image description here

3 个答案:

答案 0 :(得分:1)

你太近了我不明白为什么你有问题!

case...end as total_time之后,添加:

, case when hourpart >= 8 then
            case WHEN minpart = 0
                        THEN CAST((hourpart - 8) AS NVARCHAR(200)) + ':00'
                    WHEN minpart <10
                        THEN CAST((hourpart - 8) AS NVARCHAR(200)) + ':0'+ CAST(minpart AS NVARCHAR(200))
                    ELSE CAST((hourpart - 8) AS NVARCHAR(200)) + ':' + CAST(minpart AS NVARCHAR(200)) end
  else '00:00'
  end as overTime

干杯 -

答案 1 :(得分:1)

请改为尝试:

with times as (
SELECT    t1.EmplID
        , t3.EmplName
        , min(t1.RecTime) AS InTime
        , max(t2.RecTime) AS [TimeOut]
        , 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 
group by 
          t1.EmplID
        , t3.EmplName
        , t1.RecDate
)
SELECT EmplID
,EmplName
,InTime
,[TimeOut]
,[DateVisited]
,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) totaltime
,convert(char(5), case when TimeOutSub - InTimeSub >= '08:01' then 
cast(TimeOutSub - dateadd(hour, 8, InTimeSub) as time) else '00:00' end, 108) as overtime
FROM times

答案 2 :(得分:1)

--You can create a separate function to calculate work-hrs and overtime   
-- Try this

CREATE FUNCTION GetWorkHours (
@INTime AS DATETIME
,@OutTime AS DATETIME
,@WorkingHrsINMinutes AS INT
)
RETURNS @WorkHours TABLE (
WorkHours VARCHAR(5)
,OTHours VARCHAR(5)
)
AS
BEGIN
    INSERT INTO @WorkHours
    SELECT CAST((DATEDIFF(Minute, @INTime, @OutTime)) / 60 AS VARCHAR(2)) + ':' +     CAST((DATEDIFF(Minute, @INTime, @OutTime)) % 60 AS VARCHAR(2)) AS TotalTime
    ,CASE 
        WHEN DATEDIFF(Minute, @INTime, @OutTime) > @WorkingHrsINMinutes
            THEN CAST((DATEDIFF(Minute, @INTime, @OutTime) -     @WorkingHrsINMinutes) / 60 AS VARCHAR(2)) + ':' + CAST((DATEDIFF(Minute, @INTime, @OutTime)     - @WorkingHrsINMinutes) % 60 AS VARCHAR(2))
        ELSE '00:00'
        END AS OverTime

    RETURN
END

--- Sample
SELECT *
FROM Dbo.GetWorkHours('2014-01-22 10:00:09.270', '2014-01-22 18:35:09.270', '480')

SELECT *
FROM Dbo.GetWorkHours('2014-01-22 10:00:09.270', '2014-01-22 17:35:09.270', '480')