SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query help


Query help

Author
Message
SQL_Surfer
SQL_Surfer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1576 Visits: 1098
Those days employee logged in but didn't logout.
ColdCoffee
ColdCoffee
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8455 Visits: 5555
Then this?

DECLARE @TableA TABLE 
(
EmployeeID INT
,EmployeeLoginDateTime DATETIME
,JobFunction VARCHAR(25)
)


INSERT @TableA
SELECT 123,'2012-02-01 01:00:24','Coding'
UNION ALL SELECT 123,'2012-02-01 3:00:00' ,'Programming'
UNION ALL SELECT 123,'2012-02-01 5:00:00' ,'LogOut'

UNION ALL SELECT 234,'2012-02-01 01:00:24','Coding'
UNION ALL SELECT 234,'2012-02-01 3:00:00' ,'Programming'
UNION ALL SELECT 234,'2012-02-01 5:00:00' ,'Coding'
UNION ALL SELECT 234,'2012-02-02 01:00:24','Coding'
UNION ALL SELECT 234,'2012-02-02 3:00:00' ,'Programming'
UNION ALL SELECT 234,'2012-02-02 5:00:00' ,'Coding'
UNION ALL SELECT 234,'2012-02-03 5:00:00' ,'Coding'
UNION ALL SELECT 234,'2012-02-04 5:00:00' ,'Coding'
UNION ALL SELECT 234,'2012-02-04 7:00:00' ,'Logout'

UNION ALL SELECT 567,'2012-02-01 3:00:00' ,'Programming'
UNION ALL SELECT 567,'2012-02-01 5:00:00' ,'LogOut'
UNION ALL SELECT 567,'2012-02-02 3:00:00' ,'Programming'
UNION ALL SELECT 789,'2012-02-01 3:00:00' ,'Programming'

SELECT EmployeeID
,CrsApp.EmployeeLoginDateTime
,'Missed LogOut' JobFunction
FROM @TableA OutTab
CROSS APPLY ( SELECT DATEADD(DD,DATEDIFF(DD,0,OutTab.EmployeeLoginDateTime),0) ) CrsApp (EmployeeLoginDateTime)
WHERE NOT EXISTS
( SELECT 1
FROM @TableA InrTab
WHERE InrTab.JobFunction = 'LogOut'
AND InrTab.EmployeeID = OutTab.EmployeeID
AND DATEADD(DD,DATEDIFF(DD, 0 ,InrTab.EmployeeLoginDateTime),0) = CrsApp.EmployeeLoginDateTime)
GROUP BY EmployeeID
,CrsApp.EmployeeLoginDateTime


ColdCoffee
ColdCoffee
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8455 Visits: 5555
Another solution

; WITH CTE AS
(
SELECT EmployeeID
,EmployeeLoginDateTime = DATEADD(DD,DATEDIFF(DD,0,OutTab.EmployeeLoginDateTime),0)
,JobFunction = CASE WHEN JobFunction = 'Logout' THEN 1
ELSE 0
END
FROM @TableA OutTab
)
SELECT EmployeeID
, EmployeeLoginDateTime
, 'Missed LogOut' Comment
FROM CTE
GROUP BY EmployeeID , EmployeeLoginDateTime
HAVING SUM(JobFunction) = 0



This solution is definitely faster than the previous one!

Table '#32AB8735'. Scan count 2, logical reads 17, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 86 ms.
Table '#32AB8735'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 65 ms.

SQL_Surfer
SQL_Surfer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1576 Visits: 1098
Yep. Thats it. Thanks for your help.
ColdCoffee
ColdCoffee
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8455 Visits: 5555
SQL_Surfer (3/14/2012)
Yep. Thats it. Thanks for your help.

You're welcome.. One question - did u use the first one or the second?
SQL_Surfer
SQL_Surfer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1576 Visits: 1098
Ran the first one and got the result I was expecting. Since, you mentioned 2nd one was effecient, I will use the second one. Thanks again!!!
SQL_Surfer
SQL_Surfer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1576 Visits: 1098
OK. What if employee's shift starts around 11PM and goes all the way till next morning. How would that change the query?
ColdCoffee
ColdCoffee
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8455 Visits: 5555
That query willl not handle that scenario. We can work it out anyways. Do u have any "Shift identifier"??
balajisrm
balajisrm
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 93
Try this query

select employeeid, employeelogindatetime, employeedept
from tablename
where employeelogindatetime between @fromdate and @todate
and employeelogouttime like isnull('',null)
SQL_Surfer
SQL_Surfer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1576 Visits: 1098
No, we don't have shift identifier.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search