in MySql I have this kind of table (datas):
What I want to do is to get date, temperature for sensor (1) , temperature for sensor (2) in a single row (group by date).
Now I have two records for the same dtg for sensor 1 and sensor 2...
Thank you
in MySql I have this kind of table (datas):
What I want to do is to get date, temperature for sensor (1) , temperature for sensor (2) in a single row (group by date).
Now I have two records for the same dtg for sensor 1 and sensor 2...
Thank you
Use GROUP_CONCAT
:
SELECT dtg, GROUP_CONCAT(temperature ORDER BY sensor_id)
FROM yourTable
GROUP BY dtg
Demo here:
You can use a self join:
SELECT
a.dtg AS dtg1,
a.sensor_id AS sensor_id1,
a.temperature AS temperature1,
b.sensor_id AS sensor_id2,
b.temperature AS temperature2
FROM yourTable AS a
LEFT JOIN yourTable AS b
ON a.dtg = b.dtg
AND NOT a.sensor_id = b.sensor_id
GROUP BY a.dtg
It's not ideal hardcoding these values in the query, but this is a workaround to transpose the rows to columns:
SELECT dtg,
COALESCE(IF(sensor_id='28-000004a9fa09', temperature, NULL)) t1,
COALESCE(IF(sensor_id='28-000004aa21cd', temperature, NULL)) t2
FROM readings
GROUP BY dtg
You might need to play with the aggregates if you start getting unpredictable time readings. Especially given the DB18B20s can sometimes take a while to read, it's possible you might get the seconds overlapping.