SELECT *, SUBDATE(date,INTERVAL RANK() over (PARTITION bystate ORDER BY date) day) reffrom (select fail_date date,'failed'statefrom Failedunion allselect success_date date,'succeeded'statefrom Succeeded) aWHERE date BETWEEN '2019-01-01'and'2019-12-31')
SELECT state period_state, min(date) start_date, max(date) end_datefrom (SELECT *, SUBDATE(date,INTERVAL RANK() over (PARTITION bystate ORDER BY date) day) reffrom (select fail_date date,'failed'statefrom Failedunion allselect success_date date,'succeeded'statefrom Succeeded ORDER BY date) a WHERE date BETWEEN '2019-01-01'and'2019-12-31') bGROUP BY state, refORDER BY start_date;