So I have a set of data that includes a column of dates and an ID column. Here's a sample of the date data:
5/30/10 12:00 AM
5/30/10 12:01 AM
5/30/10 1:59 AM
5/30/10 1:59 AM
5/30/10 3:58 AM
5/30/10 3:58 AM
5/30/10 5:57 AM
5/30/10 6:57 AM
5/30/10 7:56 AM
5/30/10 7:56 AM
5/30/10 9:55 AM
5/30/10 11:54 AM
What I'd like to do is create buckets for these rows based on a parameter like "2 hours". These two hour windows would start at the earliest date in the dataset, but would jump to the next starting time as you scanned through the list. For example, the expected output of "buckets" for my list would be:
5/30/10 12:00 AM 1
5/30/10 12:01 AM 1
5/30/10 1:59 AM 1
5/30/10 1:59 AM 1
5/30/10 3:58 AM 2
5/30/10 3:58 AM 2
5/30/10 5:57 AM 2
5/30/10 6:57 AM 3
5/30/10 7:56 AM 3
5/30/10 7:56 AM 3
5/30/10 8:55 AM 3
5/30/10 11:54 AM 4
So you can see that when I get to 3:58 AM, it's in the 2nd group because it's more than 2 hours past 12:00 AM. However 5:57 AM is still in the 2nd group even though it's more than 4 hours past 12:00 AM because the 2nd group base time is 3:58 AM, not 2:00 AM.
I've tried to create the grouping column by using a partition function like this:
FLOOR(DATEDIFF(SECOND, t.BaseCreateDate, t.CreateDate) / t.DedupWindow)
Where BaseCreateDate is the earliest date in my set, CreateDate is the data I listed, and DedupWindow is the 2 hours. However, that gives me fixed 2 hour windows and I can't seem to find math that resets the base as needed through the data.
I have this working in a cursor, but for a couple of reasons I'd like to get it working set based.