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 |
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-28 : 01:54:37
|
Friends,I have to select last 5 days login data from UserLog Table, Based on the LogMessage, Please find below example for clarityTable : UserLogSample Data :LogId | UserID | IP | DateTime | LogMessage1 | 1012 | 102.34.23.xx | 2013-10-22 08:42:00 | User ID 1012 (Soft Token)[] - Primary authentication successful from RDS2 | 1012 | 102.34.23.xx | 2013-10-22 08:43:00 | User ID 1012 (Soft Token)[] - Network Connect: Session started from RDS Location3 | 1012 | 102.34.23.xx | 2013-10-22 08:45:00 | User ID 1012 (Soft Token)[] - Network Session Initiated: Success Session from RDS Location4 | 1015 | 102.xx.203.xx | 2013-10-22 09:42:00 | User ID 1015 (Soft Token)[] - Primary authentication successful from RDS5 | 1015 | 102.xx.203.xx | 2013-10-22 09:43:00 | User ID 1015 (Soft Token)[] - Network Connect: Session started from RDS Location6 | 1015 | 102.xx.203.xx | 2013-10-22 09:45:00 | User ID 1015 (Soft Token)[] - Network Session Initiated: Success Session from RDS Location7 | 1012 | 102.34.23.xx | 2013-10-22 09:52:00 | User ID 1012 (Soft Token)[] - Primary authentication successful from RDS8 | 1012 | 102.34.23.xx | 2013-10-22 09:53:00 | User ID 1012 (Soft Token)[] - Network Connect: Session started from RDS Location9 | 1012 | 102.34.23.xx | 2013-10-22 09:55:00 | User ID 1012 (Soft Token)[] - Network Session Initiated: Success Session from RDS Location10 | 1012 | 102.34.23.xx | 2013-10-23 11:52:00 | User ID 1012 (Soft Token)[] - Primary authentication successful from RDS11 | 1012 | 102.34.25.xx | 2013-10-23 11:54:00 | User ID 1012 (Soft Token)[] - Network Connect: Session started from RDS Location12 | 1012 | 102.34.25.xx | 2013-10-23 11:55:00 | User ID 1012 (Soft Token)[] - Network Session Initiated: Success Session from RDS LocationExpectedResult:I would like to select the user loged data by UserID for last 5 days, I will pass the UserID as a paramter, Time taken should be calculated based on LogMessage ( Time Between "Primary authentication successful" message and "Network Connect: Session started " Message), If multiple login for the same day We have to take the most recent one for the day.Input : @UserID = 1012UserID | Date | IP Address | TimeTaken (Min)1012 | 2013-10-22 | 102.34.23.xx | 11012 | 2013-10-23 | 102.34.25.xx | 2 Please let me know if you want more details to get my expected output. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-28 : 07:13:57
|
[code]SELECT UserID,DATEADD(dd,DATEDIFF(dd,0,[DateTime]),0) AS [Date],IPAddress,DATEDIFF(minute,MAX(CASE WHEN LogMessage LIKE 'Primary authentication successful%' THEN [DateTime] END),MAX(CASE WHEN LogMessage LIKE 'Network Connect: Session started %' THEN [DateTime] END)) AS TimeTakenFROM(SELECT ROW_NUMBER() OVER (PARTITION BY UserID,DATEDIFF(dd,0,[DateTime]),LogMessage ORDER BY [DateTime] DESC) AS Seq,*FROM UserLogWHERE UserID = @UserIDAND [DateTime] >= DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-5))tWHERE Seq=1GROUP BY UserID,DATEADD(dd,DATEDIFF(dd,0,[DateTime]),0),IPAddress[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-28 : 08:26:03
|
Hey Thanks Lot, Works charm... |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-28 : 09:06:20
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|