Days off between shifts

easySQL~15 min · 4 tests · ridelog

driver_days has one row per day a driver was online. A missing day is a day off.

Ops wants to see every break: the last day a driver was online before it, the day they came back, and how many days they were off in between.

Return one row per break with:

column type meaning
driver_id integer the driver
last_online date the online day just before the break
back_online date the first online day after the break
days_off integer days between the two, not counting either end

Online on the 3rd and back on the 8th is a break of 4 days off (the 4th to the 7th).

Edge cases:

  • Two online days in a row are not a break. Leave them out.
  • Days off before a driver’s first online day or after their last don’t count. There’s nothing on both sides.
  • The same day can appear twice for a driver. Treat it as one online day.

Row order doesn’t matter.

Tables: driver_days.

Learn the ideaRead the lesson →
solution.sqlDuckDB · ridelog

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run