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 |
|
knox203
Starting Member
31 Posts |
Posted - 2009-11-16 : 14:09:47
|
Hello all,I have a simple query which grabs all chat sessions for the day. That is all fine and well, but I'd like to find the difference (in seconds) between each message, and remove that block of time from the total chat session if the difference is more than 5 minutes but leave the rest.Here's the query:SELECT C.[SessionIdTime], C.[Date]FROM LcsLog.dbo.ConferenceMessages AS C INNER JOIN LcsLog.dbo.Users AS U ON C.FromId = U.UserIdWHERE (DATEADD(hh, -8, C.SessionIdTime) BETWEEN CONVERT(VARCHAR, GETDATE(), 101) AND CONVERT(VARCHAR, DATEADD(d, 1, GETDATE()), 101))ORDER BY C.[SessionIdTime], C.[Date] Here's the results:SessionIdTime Date2009-11-16 15:58:27.610 2009-11-16 15:58:27.6102009-11-16 16:03:05.767 2009-11-16 16:03:05.7672009-11-16 16:03:05.767 2009-11-16 16:29:38.9202009-11-16 16:03:05.767 2009-11-16 16:48:00.8602009-11-16 16:03:05.767 2009-11-16 16:53:36.0302009-11-16 16:03:05.767 2009-11-16 16:54:27.2802009-11-16 16:18:45.827 2009-11-16 16:18:45.8272009-11-16 16:18:45.827 2009-11-16 17:08:32.5172009-11-16 16:18:45.827 2009-11-16 17:13:43.8432009-11-16 16:20:50.437 2009-11-16 16:20:50.4372009-11-16 16:20:50.437 2009-11-16 16:22:09.8132009-11-16 16:25:00.967 2009-11-16 16:25:00.9672009-11-16 16:28:24.250 2009-11-16 16:28:24.2502009-11-16 16:31:47.657 2009-11-16 16:31:47.6572009-11-16 16:31:47.657 2009-11-16 16:31:50.0002009-11-16 16:31:47.657 2009-11-16 16:32:30.6572009-11-16 16:31:47.657 2009-11-16 16:35:10.733 I'm not sure where to approach this task. I've thought about doing it in PHP but I figured it might be less code and less strain on our web server to just do it in my SQL query. Thank you all for your time.- AdamEDIT: Sorry, forgot to point out that the 'Date' column is the message sent timestamp. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-11-16 : 14:29:29
|
| You haven't actually given us enough info about your tables to answer your question..DDLs and sample data would be helpfulbut this should get you started.JimDECLARE @Table TABLE (SessionTime datetime,Date datetime)INSERT INTO @tableSELECT '2009-11-16 15:58:27.610','2009-11-16 15:58:27.610' UNION ALLSELECT '2009-11-16 16:03:05.767','2009-11-16 16:03:05.767' UNION ALLSELECT '2009-11-16 16:03:05.767','2009-11-16 16:29:38.920' UNION ALLSELECT '2009-11-16 16:03:05.767','2009-11-16 16:48:00.860' UNION ALLSELECT '2009-11-16 16:03:05.767','2009-11-16 16:53:36.030' UNION ALLSELECT '2009-11-16 16:03:05.767','2009-11-16 16:54:27.280' UNION ALLSELECT '2009-11-16 16:18:45.827','2009-11-16 16:18:45.827' UNION ALLSELECT '2009-11-16 16:18:45.827','2009-11-16 17:08:32.517' UNION ALLSELECT '2009-11-16 16:18:45.827','2009-11-16 17:13:43.843' UNION ALLSELECT '2009-11-16 16:20:50.437','2009-11-16 16:20:50.437' UNION ALLSELECT '2009-11-16 16:20:50.437','2009-11-16 16:22:09.813' UNION ALLSELECT '2009-11-16 16:25:00.967','2009-11-16 16:25:00.967' UNION ALLSELECT '2009-11-16 16:28:24.250','2009-11-16 16:28:24.250' UNION ALLSELECT '2009-11-16 16:31:47.657','2009-11-16 16:31:47.657' UNION ALLSELECT '2009-11-16 16:31:47.657','2009-11-16 16:31:50.000' UNION ALLSELECT '2009-11-16 16:31:47.657','2009-11-16 16:32:30.657' UNION ALLSELECT '2009-11-16 16:31:47.657','2009-11-16 16:35:10.733';with Cte_Times(RowNumber,SessionTime)AS(SELECT [RowNumber] = ROW_Number() over(order by sessiontime asc),sessiontimeFROM @table)select distinct t1.sessiontime--,datediff(mi,t1.sessiontime,t2.sessiontime) from Cte_Times t1inner join Cte_Times t2on t1.rownumber = t2.rownumber - 1 WHERE datediff(mi,t1.sessiontime,t2.sessiontime) <5Everyday I learn something that somebody else already knew |
 |
|
|
knox203
Starting Member
31 Posts |
Posted - 2009-11-16 : 15:33:57
|
| jimf,Thank you very much for your response. I was able to create a solution in a way that is more familiar to me thanks to your code. I appreciate the time you spent to write that all out!- Adam |
 |
|
|
|
|
|
|
|