I've been looking for and thinking of a solution to the following but I cant come up with it, and hope someone can help me.
I have the following table:
ClientId RecordDateTime
-------- -------------------
1 2019-02-14 14:05:49
1 2019-02-14 14:06:34
1 2019-02-14 14:07:19
1 2019-02-14 14:08:49
1 2019-02-14 14:09:34
1 2019-02-14 14:10:32
1 2019-02-14 14:12:32
1 2019-02-14 14:14:18
1 2019-02-14 14:15:10
I need to count the amount of occurrences using a determined amount of minutes to split the occurrences. For example:
- I set the limit to 1 minutes
- First datetime is 14:05:49
- Second datetime is 14:06:34, which has less than 1 minute difference with the previous one (14:05:49), so I consider it belongs to the same occurrence.
Third datetime is 14:07:19, which also has less than 1 minute difference with the previous one (14:06:34), so is still same occurrence
Now it comes 14:08:49, which has a difference with the previous one of MORE than 1 minute, so I consider its a new occurence.
- Next 14:09:34, which has a difference of less than 1 minute with 14:08:49.
And so it goes.
At the end, the result I want is:
ClientId Occurrence Start Occurrence End
-------- ------------------- -------------------
1 2019-02-14 14:05:49 2019-02-14 14:07:19
1 2019-02-14 14:08:49 2019-02-14 14:10:32
1 2019-02-14 14:12:32 2019-02-14 14:12:32
1 2019-02-14 14:14:18 2019-02-14 14:15:10
This is just an example, but I have a lot of data, from multiple ClientIds
.
Is there way of doing this without using stored procedures and looping through each row?
Thanks in advance for any help