Re: Enhancement request: enable FIRST/LAST_value() also as a regularaggregate (not only as windowing function)

Поиск
Список
Период
Сортировка
От Robert Haas
Тема Re: Enhancement request: enable FIRST/LAST_value() also as a regularaggregate (not only as windowing function)
Дата
Msg-id CA+TgmobGc1mLxuE6ToWv5_i7VgoE9pP42med0ebQPcPrvZ4+2A@mail.gmail.com
обсуждение исходный текст
Ответ на Enhancement request: enable FIRST/LAST_value() also as a regularaggregate (not only as windowing function)  (matshyeq <matshyeq@gmail.com>)
Ответы Re: Enhancement request: enable FIRST/LAST_value() also as a regularaggregate (not only as windowing function)  (matshyeq <matshyeq@gmail.com>)
Список pgsql-hackers
On Tue, Jul 24, 2018 at 4:16 PM, matshyeq <matshyeq@gmail.com> wrote:
I'd like to throw here an enhancement proposal to discuss/consider.
The FIRST/LAST_value() functions offer powerful lookup capabilities, eg.
here

SELECT t.* ,FIRST_value(v1)OVER(PARTITION BY gid ORDER BY v2) fv ,LAST_value(v1)OVER(PARTITION BY gid ORDER BY v2 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) lv FROM( VALUES (1, 'b', 3),(1, 'd', 1),(1, 'a', 2) ,(2, 'x', 7),(2, 'y', 9),(2, 'z', 8),(2, 'v', 9)) t (gid, v1, v2);




but, given those values are repeating - why can't I simply use this functions as regular aggregates?

Or can I? It doesn't seem to be possible while I find this use case actually more common than in windowing context…
Am I missing some workaround here?

Why not just define a custom aggregate function that does whatever you need?  I don't think it would be too hard.  e.g. for something like LAST_VALUE() just make the transition type equal to the output type and save the last value you've seen thus far as the transition value.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Robert Haas
Дата:
Сообщение: Re: 11beta crash/assert caused by parameter type changes
Следующее
От: Tom Lane
Дата:
Сообщение: Re: 11beta crash/assert caused by parameter type changes