josiete.com

Time Series

SQL Server PIVOT: turning sensor readings into one row per instant

Temperature, humidity, and pressure readings move from one row per metric into separate columns

Imagine receiving sensor readings every five minutes. Each message contains a time, a sensor ID, a metric name, and a value. Storing messages as they arrive is convenient: if a new metric appears tomorrow, it simply adds more rows. But a report consuming the data wants something else: one row per time and sensor, with temperature, humidity, and pressure in separate columns.