AnalystPath

Pivot Sensor Readings Into One Row Per Device

SQLHardSenior level~15 min

Problem

An IoT platform stores readings in a tall format. Table `readings` has `(device_id, gauge, value)`, where `gauge` is the name of a measurement station and `value` is the reading that device produced at that station.

Reshape the data into a wide format: output `device_id` as the first column, then one column for each distinct gauge name (sorted alphabetically), holding that device's `value` at that gauge, or NULL if the device has no reading there. There is at most one reading per (device, gauge).

For this dataset the gauges are: `Alpha`, `Bravo`, `Delta`, `Echo`. Report one row per device.

Tables

Example rows — the live problem includes the full dataset.

readings
device_idgaugevalue
1Alpha12
1Delta40
2Bravo7

Expected output

Your answer should return 3 rows with the columns device_id, Alpha, Bravo, Delta, Echo.

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