Problems / Long to Wide: Sensor Readings / Editorial
EAV (entity-attribute-value) storage keeps one row per station per metric — flexible to write, awkward to read. To widen it:
GROUP BY station_id
MAX(CASE WHEN metric = 'temp' THEN value END) AS temp
In PySpark the same shape is F.max(F.when(F.col("metric") == "temp", F.col("value"))) inside .agg().
F.max(F.when(F.col("metric") == "temp", F.col("value")))
.agg()
MAX-of-one-value is the standard idiom for pivoting EAV data when each pivot cell holds a single value rather than a true aggregate. The tempting alternative — self-joining readings three times, once per metric — needs LEFT JOINs to survive the station with the offline pressure sensor; conditional aggregation gets that NULL right for free.
readings
Solve Long to Wide: Sensor Readings yourself →