My table has the following structure:
sensor_id | timestamp | value
-----------|------------|-------
1 | 1516905900 | 100.1
4 | 1516916920 | 90.4
... | ... | ...
I need to select the columns with the max value for each day for a given sensor_id. I need the timestamp associated with each max value in the results.
I have had some success using MAX() and GROUP BY and DATE_FORMAT, but I cannot get my query to include the timestamp associated with the max value for each group.
This is what I have so far:
select DATE_FORMAT(from_unixtime(timestamp), '%m/%d/%Y') as x, max(value) from sensor_log where sensor_id=6 group by x;
The result only includes the x and max(value) columns. I think it might be possible to use some sort of JOIN to get the rest of the columns, but I haven't been able to figure it out.
Another issue I can foresee is the possibility of a sensor_id having multiple occurrences of the max value on the same day, in this case I would only like to keep the first occurrence of that value for that day.