Ok, I have a database table of rows with a StartDate and an EndDate. What I need to do is return blocks of consumed time from that.
So, for example, if I have 3 rows as follows:
RowID StartDate EndDate
1 2011-01-01 2011-02-01
2 2011-01-30 2011-02-20
3 2011-03-01 2011-04-01
then the blocks of used time would be as follows:
2011-01-01 to 2011-02-20 and 2011-03-01 to 2011-04-01
Is there an easy method of extracting that from a MySql database? Any suggestions welcome!
Have you tried mysql group concat
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
It would return a comma separated string, but you would still have to intialize that as an array in your application.
Look at the diagram below which represents some overlapping time periods
The beginning or end of any contiguous time period, marked with an
X
doesn't fall inside any other time period. If we identify these times we can make some progress.This query identifies the boundaries.
The result of this query on your data is
The date ranges you are after are between consecutive lower and upper bounds shown abouve. With a bit of post processing, these can easily be found.