Jeg har kigget på dit "mere komplekse problem" (arbejder stadig på et svar), men her er en løsning på dette problem. I betragtning af at du bruger vinduesfunktioner, antager jeg, at du bruger MySQL 8 og så også kan bruge CTE'er:
WITH cte AS (SELECT DATE(`date_time`) AS `date`,
`data`,
MAX(`data`) OVER (ORDER BY `date_time` ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS `data_max`
FROM `test`),
cte2 AS (SELECT `date`,
`data`,
`data_max`,
CASE WHEN `data` < `data_max` THEN `data` - `data_max` END AS `data_diff`
FROM cte)
SELECT `data_max`,
MIN(CASE WHEN `data_diff` IS NOT NULL THEN date END) AS diff_date_from,
MAX(CASE WHEN `data_diff` IS NOT NULL THEN date END) AS diff_date_to
FROM cte2
GROUP BY `data_max`
HAVING diff_date_from IS NOT NULL
Output:
data_max diff_date_from diff_date_to
4 2017-01-04 2017-01-06
5 2017-01-09 2017-01-11
6 2017-01-13 2017-01-13