Search Postgresql Archives

Re: Can any_value be used like first_value in an aggregate?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bruno Wolff III <bruno@xxxxxxxx> 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





[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux