SQL Server 2008: I’m having a tough time translating my pseudo SQL to actual execution. In short, I have a data set that has:
id, startdate, enddate
so, for example:
1,1/1/2010,2/1/2010
1,3/1/2010,3/15/2010
2,4/1/2010,6/1/2010
2,5/1/2010,5/15/2010
2,7/1/2010,7/15/2010
Where each ID could be listed multiple times and the dates could overlap.
I need to:
a) Get the consecutive days for each ID. So, for example:
1 = 1/1/2010 thru 2/1/2010 + 3/1/2010 through 3/15/2010 = 43
2 = 4/1/2010 thru 6/1/2010 + 7/1/2010 through 7/15/2010 (note: the 5/1/10 thru 5/15 was omitted because it overlapped)
My initial thought was to write a sql query that would do this:
Date, ID, Active
I’m pulling data for an entire year, so I would have a date/ID entry for each ID in the table. The “active” would be a 1 or 0 depending on whether that ID was “on” for that particular day.
Then I could group that up and get my “distinct” cumulative for the year.
Problem is, that will create a table with over 300,000,000 million records, and I just can’t imagine there isn’t a better way to do this.
Any advice would be greatly appreciated.
If I understand your question correctly, this will give you your results