I have a simple table with columns start_dateand end_date. These date values may overlap.
id start_date end_date
1 2011-01-01 2012-04-01
2 2012-05-01 2013-10-01
3 2013-09-01 2014-09-01
4 2013-10-01 2014-08-01
5 2013-12-01 2014-11-01
6 2013-09-01 2014-09-01
7 2015-01-01 2015-11-01
The problem is to find the amount in months. Example:
id: 2,3,4,5,6overlaps, so the idea is to take MAX end_dateand MIN start_dateof 2,3,4,5,6and add a date difference of 1 and 7.
At this time: I found how to estimate the date difference in months:
PERIOD_DIFF( DATE_FORMAT(end_date, '%Y%m') , DATE_FORMAT(start_date, '%Y%m') )
I know the idea here is this:
- Understand whether two dates coincide or not. And if so, then change the dates accordingly (adjust the end dates and start dates).
- Scrolling through all dates, estimating the difference in dates in months, the amount and the final result of the return.
, , . , - , MySQL-.