aggregate version of first_value function?

Поиск
Список
Период
Сортировка
Искать

aggregate version of first_value function?

От:
Itagaki Takahiro <itagaki.takahiro@gmail.com>
Дата:
We have window function version of first_value(),
but aggregate version looks useful to write queries something like:

=# CREATE TABLE obj (id integer, pos point);
=# SELECT X.id,         first_value(Y.id ORDER BY X.pos <-> Y.pos) AS neighbor  FROM obj X, obj Y  GROUP BY X.id;

Is it reasonable? Or, do we have alternative ways for the same purpose?

-- 
Itagaki Takahiro

Re: aggregate version of first_value function?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Itagaki Takahiro  writes:
> We have window function version of first_value(),
> but aggregate version looks useful to write queries something like:

> =# CREATE TABLE obj (id integer, pos point);
> =# SELECT X.id,
>           first_value(Y.id ORDER BY X.pos <-> Y.pos) AS neighbor
>    FROM obj X, obj Y
>    GROUP BY X.id;

> Is it reasonable? Or, do we have alternative ways for the same purpose?

I don't see any good reason to encourage people to write that in a
nonstandard way when there's a prefectly good standard way, ie,
use the window-function version.
		regards, tom lane

FAQ