In my project, I need to calculate the time span. I am currently retrieving every TimeStamp that matches my query and stores them in a List<>
. I then iterate over the list to see if there are intervals of 10 seconds or less, and then I add them together. Anything greater than 10 seconds is ignored. I am wondering if there is an SQL query that I can do that will do this for me? I did a few searches but found nothing. In fact, I would not want to store so much information in my memory if I do not need it. Here is the method I use to iterate through my List<>
:
private static TimeSpan TimeCalculations(IList<DateTime> timeStamps) { var interval = new TimeSpan(0, 0, 10); var totalTime = new TimeSpan(); for (var j = 0; j < timeStamps.Count - 1; j++) { if (timeStamps[j + 1].Subtract(timeStamps[j]) > interval) continue; var timeDifference = timeStamps[j + 1].Subtract(timeStamps[j]); totalTime = totalTime.Add(timeDifference); } return totalTime; }
The data that is currently being retrieved can be from 10 to 400 thousand rows of data. Here is an example:
2006-09-07 11:46:09 2006-09-07 11:46:19 - 10 seconds 2006-09-07 11:46:20 - 1 second 2006-09-07 11:46:36 2006-09-07 11:47:49 2006-09-07 11:47:53 - 4 seconds 2006-09-07 11:48:02 - 9 seconds 2006-09-07 11:48:15 2006-09-07 11:48:29 2006-09-07 11:48:34 - 5 seconds 2006-09-07 11:54:29 2006-09-07 11:54:39 - 10 seconds 2006-09-07 11:54:49 - 10 seconds 2006-09-07 11:54:59 - 10 seconds
This will result in approximately 59 seconds. This is the result I'm looking for.
I am using a SQLite database.
EDIT
Looking at the answers, I can say that my question was not complete enough. My current request for TimeStamps is sufficient. I am looking for a request to add the difference between them if the interval is 10 seconds or less.
source share