You can do it this way.
Combine the previous day with the current day and yesterday with the day before. This join would look like this:
SELECT *
FROM table AS t1 INNER JOIN table AS t2 ON t1.date = SUBDATE(t2.date, 1) --or whatever you need to get the previous day
You will now notice that unpaired days are on the boundaries, while paired days are on the inside of consecutive intervals. To obtain the boundaries you have to use an outer join.
SELECT *
FROM table AS t1 FULL OUTER JOIN table AS t2 ON t1.date = SUBDATE(t2.date, 1)
Now what you need is to select just those unpaired dates:
SELECT *
FROM table AS t1 FULL OUTER JOIN table AS t2 ON t1.date = SUBDATE(t2.date, 1)
WHERE t1.date IS NULL OR t2.date IS NULL;
When t1.date IS NULL
it is a starting day. When t2.date IS NULL
it is an ending day.
Just take the MAX()
of what you need:
SELECT MAX(t2.date)
FROM table AS t1 FULL OUTER JOIN table AS t2 ON t1.date = SUBDATE(t2.date, 1)
WHERE t1.date IS NULL;
DISCLAIMER I jotted down this answer on the fly, the -1
, IS NULL
or MIN()
vs MAX()
may be wrong but the basic idea should be clear.
As an explanation, what you want is this:
t1 t2
NULL 16/07/1994 <-- interesting join (start of an interval)
16/07/1994 17/07/1994
17/07/1994 18/07/1994
18/07/1994 NULL <-- interesting join (end of an interval)