How to append a column data to a MAX Aggregate function in SQL? -
column names: name, reading, channel, time
i'm trying find max value each distinct name. each name has many values read on time interval of once every hour. goal pick out max value each distinct name , record time interval occurred in.
so far have gotten code:
select name, max(reading) "max_reading", data, channel, time interval_data group name, data, channel however, gives me error saying time has aggregated or grouped by. there easy way append time max value occurred @ output , not have perform grouped by/aggregate function on 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 each distinct name time when 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
without knowing how data , channel come play, use find data need each name's max(reading):
select id.name, id.reading, id.data, id.channel, id.type interval_data id join ( select name, max(reading) reading interval_data group name ) id_agg on id.name = id_agg.name , id.reading = id_agg.reading if need channel , type distinguish unique row, include in inner-join sub-query , in join's on parameters.
Comments
Post a Comment