LEAD
function to get the next visit date for each user's visit date, partitioned by user_id
and ordered by visit_date
.IFNULL
to replace null values (which indicate that there is no subsequent visit) with the current date '2021-01-01'.DATEDIFF
.user_id
and use the MAX
function to find the biggest window of days for each user.user_id
and the calculated biggest window as the final output, ordered by user_id
.erDiagram UserVisits { int user_id date visit_date }