Column names: name
, reading
, channel
, time
I'm currently trying to the find the max value for each distinct name
. Each name
has many values as it is read on a time interval of once every hour. The goal is to pick out the max value for each distinct name
and record the time interval that it occurred in.
So far I have gotten this code:
SELECT name, max(reading) AS "max_reading", data, channel, time
FROM interval_data
GROUP BY name, data, channel
However, this gives me an error saying that time
has to be aggregated or grouped by. Is there any easy way to append the time that the max value occurred at to the output and not have to perform a grouped by/aggregate function on the column?
Sample Data:
NAME READING DATA CHANNEL TIME
1 1 1 1 1/15/2015 09:00
1 3 1 1 1/15/2015 10:00
1 2 1 1 1/15/2015 11:00
1 5 1 1 1/15/2015 12:00
2 2 1 1 1/15/2015 09:00
2 4 1 1 1/15/2015 10:00
2 6 1 1 1/15/2015 11:00
2 5 1 1 1/15/2015 12:00
3 7 1 1 1/15/2015 09:00
3 3 1 1 1/15/2015 10:00
3 5 1 1 1/15/2015 11:00
3 2 1 1 1/15/2015 12:00
Desired Output: (Max READING for each distinct NAME with TIME when the max READING reading occurred)
NAME READING DATA CHANNEL TIME
1 5 1 1 1/15/2015 12:00
2 6 1 1 1/15/2015 11:00
3 7 1 1 1/15/2015 09:00