Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
WebKill
Starting Member
32 Posts |
Posted - 2014-02-12 : 02:32:43
|
I have done some research and found some examples, I think I am close but cannot pin this down. I need to find the tiem difference between start and end records, but currently it finds a match for each start record to every end record after it instead of 1 to 1.Example:DECLARE @DataTable TABLE(ActivityTime DATETIME,ActivityAction VARCHAR(10),UserName VARCHAR(10))INSERT INTO @DataTable VALUES('2014-02-10 11:18:30.767', 'Open', 'BOB'), ('2014-02-10 11:18:30.883', 'Close', 'BOB'),('2014-02-10 11:19:19.557', 'Open', 'BOB'), ('2014-02-10 11:19:19.667', 'Close', 'BOB'),('2014-02-10 11:19:56.190', 'Open', 'JIM'), ('2014-02-10 11:19:56.297', 'Close', 'JIM'),('2014-02-10 11:21:31.213', 'Open', 'BOB'), ('2014-02-10 11:21:32.133', 'Close', 'BOB')SELECT Start_Log.ActivityTime , End_Log.ActivityTime, DATEDIFF(MILLISECOND,Start_Log.ActivityTime, End_Log.ActivityTime) as 'Time Difference' ,Start_Log.UserName from @DataTable as Start_Log join @DataTable End_LogON Start_Log.UserName = End_Log.UserNameWHERE Start_Log.ActivityTime < End_Log.ActivityTimeAND Start_Log.ActivityAction = 'Open'AND End_Log.ActivityAction = 'Close' Currently it gives this:2014-02-10 11:18:30.767 | 2014-02-10 11:18:30.883 | 116 | BOB2014-02-10 11:18:30.767 | 2014-02-10 11:19:19.667 | 48900 | BOB2014-02-10 11:19:19.557 | 2014-02-10 11:19:19.667 | 110 | BOB2014-02-10 11:19:56.190 | 2014-02-10 11:19:56.297 | 106 | JIM2014-02-10 11:18:30.767 | 2014-02-10 11:21:32.133 | 181366 | BOB2014-02-10 11:19:19.557 | 2014-02-10 11:21:32.133 | 132576 | BOB2014-02-10 11:21:31.213 | 2014-02-10 11:21:32.133 | 920 | BOB I need it to give this:2014-02-10 11:18:30.767 | 2014-02-10 11:18:30.883 | 116 | BOB2014-02-10 11:19:19.557 | 2014-02-10 11:19:19.667 | 110 | BOB2014-02-10 11:19:56.190 | 2014-02-10 11:19:56.297 | 106 | JIM2014-02-10 11:21:31.213 | 2014-02-10 11:21:32.133 | 920 | BOB |
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-02-12 : 03:23:55
|
SELECT Start_Log.ActivityTime , End_Log.ActivityTime, DATEDIFF(MILLISECOND,Start_Log.ActivityTime, End_Log.ActivityTime) as 'Time Difference' ,Start_Log.UserName from @DataTable as Start_Log join @DataTable End_LogON Start_Log.UserName = End_Log.UserNameWHERE Start_Log.ActivityAction = 'Open'AND End_Log.ActivityAction = 'Close'AND LEFT( RIGHT(CONVERT(VARCHAR,Start_Log.ActivityTime ,108),8),5) = LEFT( RIGHT(CONVERT(VARCHAR,End_Log.ActivityTime ,108),8),5)Veera |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-02-12 : 06:18:08
|
[code]DECLARE @DataTable TABLE(ActivityTime DATETIME,ActivityAction VARCHAR(10),UserName VARCHAR(10))INSERT INTO @DataTable VALUES('2014-02-10 11:18:30.767', 'Open', 'BOB'), ('2014-02-10 11:18:30.883', 'Close', 'BOB'),('2014-02-10 11:19:19.557', 'Open', 'BOB'), ('2014-02-10 11:19:19.667', 'Close', 'BOB'),('2014-02-10 11:19:56.190', 'Open', 'JIM'), ('2014-02-10 11:19:56.297', 'Close', 'JIM'),('2014-02-10 11:21:31.213', 'Open', 'BOB'), ('2014-02-10 11:21:32.133', 'Close', 'BOB');With CTEAS(SELECT ROW_NUMBER() OVER (PARTITION BY UserName,ActivityAction ORDER BY ActivityTime) AS Seq,*FROM @DataTable)SELECT c1.ActivityTime,c2.ActivityTime,DATEDIFF(MILLISECOND,c1.ActivityTime,c2.ActivityTime) AS Diff,c1.UserNameFROM CTE c1JOIN CTE C2ON c2.UserName = c1.UserNameAND c2.Seq = c1.SeqAND c2.ActivityAction = 'Close'AND c1.ActivityAction = 'Open'output----------------------------------------------------------------ActivityTime ActivityTime Diff UserName----------------------------------------------------------------2014-02-10 11:18:30.767 2014-02-10 11:18:30.883 116 BOB2014-02-10 11:19:19.557 2014-02-10 11:19:19.667 110 BOB2014-02-10 11:21:31.213 2014-02-10 11:21:32.133 920 BOB2014-02-10 11:19:56.190 2014-02-10 11:19:56.297 106 JIM[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|