Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me in calculating login time and logout time in SQL Server 2014.

Please find the below table with 'LoginDetails' where in
isStatus 1 indicates Login time and 0 indicates Logout time


LoginID    EmpID     LogDetail                    isStatus

1	    111	     2015-09-18 09:29:21.637	       1
2	    111	     2015-09-18 09:49:04.030	       0
3	    112	     2015-09-18 10:31:52.247	       1
4	    112	     2015-09-18 11:32:32.410	       1
5	    113	     2015-09-18 11:32:32.410	       1
6	    113	     2015-09-18 13:34:52.153	       0
7	    112	     2015-09-18 13:50:52.153	       0


I need the output in this way:

PayrollID	Date	Login	Logout  Totaltime
111	    09/18/2015	9:29	9:49    00:20:00
112	    09/18/2015	10:31
113	    09/18/2015	11:32	13:34   02:06:00
112	    09/18/2015	11:32	13:50   02:22:00
Posted
Updated 17-Sep-15 18:55pm
v3

1 solution

If there's always a login then perhaps something like

SQL
SELECT i.*,
       (SELECT o.logdetail
	FROM LoginDetails o
        WHERE i.empid = o.empid 
        AND CONVERT(date, i.logdetail) = CONVERT(date, o.logdetail)
        AND o.logdetail > i.logdetail
        AND o.isstatus = 0
        AND o.logdetail = (SELECT MIN(o2.logdetail) 
                           FROM LoginDetails  o2
                           WHERE o2.logdetail > i.logdetail)
	   ) AS logout
FROM LoginDetails i
WHERE i.isstatus = 1


ADDITION:

Added total time in minutes. The formatting for the total time should be done at client side
SQL
SELECT a.loginid,
       a.empid,
       a.logintime,
       a.logouttime,
       DATEDIFF(MINUTE, a.logintime, a.logouttime)
FROM (
       SELECT i.loginid,
              i.empid,
              i.logdetail as logintime,
             (SELECT o.logdetail
              FROM LoginDetails o
              WHERE i.empid = o.empid
              AND CONVERT(date, i.logdetail) = CONVERT(date, o.logdetail)
              AND o.logdetail > i.logdetail
              AND o.isstatus = 0
              AND o.logdetail = (SELECT MIN(o2.logdetail)
                                 FROM LoginDetails  o2
                                 WHERE o2.logdetail > i.logdetail)
             ) AS logouttime
   FROM LoginDetails i
   WHERE i.isstatus = 1) a
 
Share this answer
 
v4
Comments
[no name] 18-Sep-15 1:18am    
thank you .. i need the totaltime also
Wendelius 18-Sep-15 1:29am    
Have a look at the modified answer.
[no name] 18-Sep-15 1:48am    
totaltime is not coming in this format 02:06:00
Wendelius 18-Sep-15 2:34am    
As I wrote, it's best practice to do the formatting on the client side, but if you want to do the formatting in the SQL just modify the DATEPART and repeat it for all elements you want to show (hour, minute,day). So something like

CAST(DATEPART(HOUR, a.logouttime - a.logintime) AS nvarchar )
+ ':'
+ CAST(DATEPART(MINUTE, a.logouttime - a.logintime) AS nvarchar )
+ ':'
+ CAST(DATEPART(SECOND, a.logouttime - a.logintime) AS nvarchar )

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