From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Bruno Wolff III <bruno(at)wolff(dot)to> |
Cc: | pgsql-general(at)lists(dot)postgresql(dot)org |
Subject: | Re: Can any_value be used like first_value in an aggregate? |
Date: | 2024-06-25 17:08:45 |
Message-ID: | 1337453.1719335325@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Bruno Wolff III <bruno(at)wolff(dot)to> writes:
> For example, is output of 10 guaranteed in the following:
> bruno=> select any_value(x order by x desc) from generate_series(1,10) as x;
> any_value
> -----------
> 10
> (1 row)
Not really. It will work that way in simple cases, but I think the
behavior stops being predictable if the input gets large enough to
induce the planner to use parallel aggregation. In any case, the
example shown isn't amazingly efficient since it'll still perform
a sort to meet the ORDER BY spec.
> The use case is that I want to return a value of one column that is paired
> with the maximum value of another column in each group when using GROUP BY.
Use window functions (i.e. first_value). This is what they're for,
and they are smart enough to do just one sort for functions sharing
a common window spec.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Yongye Serkfem | 2024-06-25 17:20:03 | Replication After manual Failover |
Previous Message | Bruno Wolff III | 2024-06-25 16:42:08 | Can any_value be used like first_value in an aggregate? |