public inbox for [email protected]  
help / color / mirror / Atom feed
Can any_value be used like first_value in an aggregate?
4+ messages / 3 participants
[nested] [flat]

* Can any_value be used like first_value in an aggregate?
@ 2024-06-25 16:42 Bruno Wolff III <[email protected]>
  2024-06-25 17:08 ` Re: Can any_value be used like first_value in an aggregate? Tom Lane <[email protected]>
  0 siblings, 1 reply; 4+ messages in thread

From: Bruno Wolff III @ 2024-06-25 16:42 UTC (permalink / raw)
  To: [email protected]

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)

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.

(There aren't going to be any NULLs in the involved columns.)

Thanks.






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Can any_value be used like first_value in an aggregate?
  2024-06-25 16:42 Can any_value be used like first_value in an aggregate? Bruno Wolff III <[email protected]>
@ 2024-06-25 17:08 ` Tom Lane <[email protected]>
  2024-06-25 19:11   ` Re: Can any_value be used like first_value in an aggregate? Bruno Wolff III <[email protected]>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2024-06-25 17:08 UTC (permalink / raw)
  To: Bruno Wolff III <[email protected]>; +Cc: [email protected]

Bruno Wolff III <[email protected]> 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






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Can any_value be used like first_value in an aggregate?
  2024-06-25 16:42 Can any_value be used like first_value in an aggregate? Bruno Wolff III <[email protected]>
  2024-06-25 17:08 ` Re: Can any_value be used like first_value in an aggregate? Tom Lane <[email protected]>
@ 2024-06-25 19:11   ` Bruno Wolff III <[email protected]>
  2024-06-26 07:50     ` Re: Can any_value be used like first_value in an aggregate? Laurenz Albe <[email protected]>
  0 siblings, 1 reply; 4+ messages in thread

From: Bruno Wolff III @ 2024-06-25 19:11 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: [email protected]

On Tue, Jun 25, 2024 at 13:08:45 -0400,
  Tom Lane <[email protected]> wrote:
>
>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.

Thanks.

>> 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.

If I do that, I'd need to do it as a subselect inside of a group by. I'm 
thinking distinct on may work and be a better way to do it. The actual 
use case is a set of tripplets returned from a query, where I want on 
row for each distinct value in the first column, paired with the value 
in the second column, for which the third column is the largest. The 
second and third columns are effectively dependent on each other, so there 
won't be any ambiguity.

Thanks for getting me thinking about some other ways to approach the problem.






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Can any_value be used like first_value in an aggregate?
  2024-06-25 16:42 Can any_value be used like first_value in an aggregate? Bruno Wolff III <[email protected]>
  2024-06-25 17:08 ` Re: Can any_value be used like first_value in an aggregate? Tom Lane <[email protected]>
  2024-06-25 19:11   ` Re: Can any_value be used like first_value in an aggregate? Bruno Wolff III <[email protected]>
@ 2024-06-26 07:50     ` Laurenz Albe <[email protected]>
  0 siblings, 0 replies; 4+ messages in thread

From: Laurenz Albe @ 2024-06-26 07:50 UTC (permalink / raw)
  To: Bruno Wolff III <[email protected]>; Tom Lane <[email protected]>; +Cc: [email protected]

On Tue, 2024-06-25 at 14:11 -0500, Bruno Wolff III wrote:
> The actual 
> use case is a set of tripplets returned from a query, where I want on 
> row for each distinct value in the first column, paired with the value 
> in the second column, for which the third column is the largest. The 
> second and third columns are effectively dependent on each other, so there 
> won't be any ambiguity.

Try

  SELECT DISTINCT ON (first_column)
         first_column,
         second_column,
         third_column
  FROM the_table
  ORDER BY first_column, third_column DESC;

Yours,
Laurenz Albe






^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2024-06-26 07:50 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-06-25 16:42 Can any_value be used like first_value in an aggregate? Bruno Wolff III <[email protected]>
2024-06-25 17:08 ` Tom Lane <[email protected]>
2024-06-25 19:11   ` Bruno Wolff III <[email protected]>
2024-06-26 07:50     ` Laurenz Albe <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox