Evil rising
Evil rising

Reputation: 442

new column to calculate overtime

I wrote and managed this query to calculate total time a person has worked per day by dateDiff function now i am stuck at one place. i want to calculate that if a person has done over time than a new column should show OVERITME in hh:mm.

I n our office total working hours are 08:00 hours, greater than 8 hours are considered overtime so e.g. if a person has worked 08:35 hours than a column should show that a person has worked 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

Output: enter image description here

Upvotes: 0

Views: 902

Answers (3)

Naveen Kumar
Naveen Kumar

Reputation: 1541

--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')

Upvotes: 1

t-clausen.dk
t-clausen.dk

Reputation: 44336

Try this instead:

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

Upvotes: 1

simon at rcl
simon at rcl

Reputation: 7344

You're so close I can't see why you're having a problem!

After the case...end as total_time, add:

, 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

Cheers -

Upvotes: 1

Related Questions