AnalystPath

Unpivot Sensor Readings Into Tall Format

SQLHardSenior level~15 min

Problem

An IoT platform stores readings in a wide format. Table `readings` has one row per device with columns `device_id`, `Alpha`, `Bravo`, `Delta`, `Echo`, where each non-id column is the reading that device produced at the gauge of that name, or NULL if there is no reading there.

Reshape the data into a tall format with exactly three columns: `device_id`, `gauge`, and `value`. Produce one row per (device, gauge) where a reading actually exists; skip any combination whose value is NULL.

Return the result in any order.

Tables

Example rows — the live problem includes the full dataset.

readings
device_idAlphaBravoDeltaEcho
11240
2755
39832

Expected output

Your answer should return 8 rows with the columns device_id, gauge, value.

Starter code (SQL)

SELECT *
FROM readings;

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

Related SQL questions