Postgres: Get value of a column corresponding to max of other column in a group

One option uses window functions in a subquery to rank the timestamps by increasing and descending value, then conditional aggregation in the outer query to bring the relevant values

select id, bucket,
    percentile_cont(0.5) within group (order by value) median_value,
    min(value) min_value, 
    max(timestamp_utc) filter(where rn_asc = 1) min_timestamp,
    max(value) max_value,
    max(timestamp_utc) filter(where rn_desc = 1) max_timestamp
from (
    select t.*, 
        row_number() over(partition by id, bucket order by value) rn_asc,
        row_number() over(partition by id, bucket order by value desc) rn_desc
    from (
        select t.*, time_bucket('60', timestamp_utc) as bucket 
        from rs.mytable t
            id in (1111,123)
            and timestamp_utc between '2020-11-05 10:00:15.748643'::timestamp 
                                  and '2020-11-05 16:35:48.750313'::timestamp
    ) t
) t
group by id, bucket 
order by id, bucket

Note that we need to compute the bucket first, and put it in the partition of the window function.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top