I'm having a bit of trouble displaying correct data from my table. Im not really sure what to search for either. Im not sure min(column) or max(column) will help me here. Lets see if i can explain my problem.
My table contains this data:
> Code (nvarchar) | DateFrom (datetime) | DateTo (datetime)
> =========================================================
> 3006 | 2014-06-18 07:00:00 | 2014-06-18 08:00:00
> 3006 | 2014-06-18 09:00:00 | 2014-06-18 22:00:00
> 1006 | 2014-06-18 07:00:00 | 2014-06-18 09:00:00
> 1006 | 2014-06-18 08:00:00 | 2014-06-18 08:30:00
> 1006 | 2014-06-18 08:10:00 | 2014-06-18 18:00:00
I am going to present this by a view. It will be grouped by Code.
What i want is this output:
> Code | DateFrom | DateTo
> =========================================================
> 3006 | 2014-06-08 07:00:00 | 2014-06-18 08:00:00
> 3006 | 2014-06-18 09:00:00 | 2014-06-18 22:00:00
> 1006 | 2014-06-18 07:00:00 | 2014-06-18 18:00:00
As you see if there are gaps between DateTo and DateFrom i want it to be presented as two rows. But if the next "DateFrom" with the same code begins before (or at same time) as DateTo ends, i want that "DateTo" to be shown instead.
I don't see how i could use the function max() or min() in this case. Because of the gaps that can be during the timeslots.
Do you guys have any clue?
Im using MS SQL 2012
Thanks in advance!
edit: as commented. Islands could be my solloution?