Premium problem87. Server Busy Time From Start and Stop Events

Hard Locked

server_events(server_id, event_type, event_at)

Events alternate 'start' and 'stop' per server, beginning with a start. Return the total busy hours in September 2026, rounded to two decimals, clipped to the month boundaries.

Two edges again. A session that started in August and stopped in September counts only from midnight on the 1st. And a trailing 'start' with no matching stop is still running, so it counts to the end of the month.

Return server_id and busy_hours, ordered by server_id. Servers with no busy time in the month do not appear.

Pairing each start with the following event rather than joining starts to stops keeps the alternation from going wrong on the unmatched tail.

Tables

server_events

server_id  event_type  event_at
---------  ----------  -------------------
1          start       2026-08-31 00:00:00
1          stop        2026-09-01 03:00:00
1          start       2026-09-10 00:00:00
1          stop        2026-09-10 06:30:00
2          start       2026-09-30 12:00:00
3          start       2026-08-01 00:00:00
3          stop        2026-08-02 00:00:00

Expected result

server_id  busy_hours
---------  ----------
1          9.50
2          12.00

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query