Unfold Shift Readings
Problem
A factory logs each machine's output reading for three daily shifts in a wide table.
```
ShiftLog
+------------+------+
| Column | Type |
+------------+------+
| machine_id | int |
| morning | int |
| afternoon | int |
| night | int |
+------------+------+
machine_id is the primary key.
Each column holds the reading for that shift, or NULL if the machine was idle that shift.
```
Reshape the table into one row per `(machine_id, shift, reading)`. Do **not** emit a row when the reading for a shift is NULL.
Return the result in any order.
Tables
Example rows — the live problem includes the full dataset.
| machine_id | morning | afternoon | night |
|---|---|---|---|
| 0 | 95 | 100 | 105 |
| 1 | 70 | 80 |
Expected output
Your answer should return 5 rows with the columns machine_id, shift, reading.
Starter code (SQL)
SELECT *
FROM ShiftLog;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