Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem with calculate total working time of an emp. where an employee can login logout in a day multiple time..
my problem is How i can calculate working hours of each employee..

here is my table data ....

EmpId Date Login/LogOutStatus

E001 02/10/2012 09:00:00 AM 1
E002 02/10/2012 09:00:00 AM 1
E001 02/10/2012 10:01:00 AM 2
E001 02/10/2012 10:25:00 AM 1
E001 02/10/2012 03:00:00 PM 2
E002 02/10/2012 03:03:00 AM 2

pls help....
i want to calculate total working time of the both employee in hours....

any help will sufficient
..
Thanks.....
Posted
Comments
Sandeep Mewara 2-Oct-12 3:57am    
And where are you stuck? What have you tried so far?
Dylan Morley 2-Oct-12 4:51am    
Moved from solution by OP:

sir i m trying to get no. of working hours of employee.... that is the problem sir...
Member 10790412 28-Aug-14 6:34am    
What is am1 and am2

Try to use something like the following.
If there is a table EployeeWorkLog with the columns [EmpID], [ActionDate] and [Status].

SQL
SELECT e.EmpID, CAST(e.[ActionDate] AS DATE) AS WorkDay,
       SUM( DATEPART( HOUR, CAST(e.[ActionDate] - s.[ActionDate] AS TIME) ) ) AS WrkHrs,
       SUM( DATEPART( MINUTE, CAST(e.[ActionDate] - s.[ActionDate] AS TIME) ) ) AS WrkMins
  FROM 
       (SELECT EmpID, ActionDate
          FROM EployeeWorkLog
         WHERE [Status] = 1 ) AS s
  JOIN (SELECT EmpID, ActionDate
          FROM EployeeWorkLog
         WHERE [Status] = 2 ) AS e
    ON e.EmpID = s.EmpID
 WHERE e.ActionDate = (SELECT TOP 1 ActionDate
                         FROM EployeeWorkLog
                        WHERE EmpID = e.EmpID
                          AND ActionDate > s.ActionDate)
 GROUP BY e.EmpID, CAST(e.[ActionDate] AS DATE)

You can combine dateparts together to get single time column
 
Share this answer
 
try this,
SQL
select EmpID,date, convert(varchar,sum(datediff(MINUTE,stm,etm))/60) + ':' + convert(varchar,sum(datediff(MINUTE,stm,etm)) % 60) as hrs from
(
    SELECT
        EmpID,
        convert(datetime,convert(varchar(10),a.date,102)) as date,
        date as etm,
        (
            SELECT top 1 date
            FROM log
            WHERE [Status] = 1 and date<= a.date and empid=a.empid and convert(datetime,convert(varchar(10),a.date,102))=convert(datetime,convert(varchar(10),date,102)) order by date desc
        ) as stm
    FROM log as a
    WHERE [Status] = 2
)as a
where stm is not null
group by empid,date


remove underlined field if you don't want date-wise working hours

Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900