Total Machine Run-Time in Days
Problem
The table `MachineLog` records power events for factory machines. Each row has a `machine_id`, a `logged_at` timestamp, and an `event` that is either `power_on` or `power_off`. For each machine the events alternate: a machine runs from a `power_on` until the very next event for that machine (a `power_off`). Add up the running time across all machines and report it as whole days, rounded down, in a single column `run_days`.
Tables
Example rows — the live problem includes the full dataset.
| machine_id | logged_at | event |
|---|---|---|
| 1 | 2024-03-01 08:00:00 | power_on |
| 1 | 2024-03-03 08:00:00 | power_off |
| 1 | 2024-03-04 06:00:00 | power_on |
Expected output
Your answer should return 1 row with the columns run_days.
Starter code (SQL)
SELECT *
FROM MachineLog;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock