Creating a non-strict custom aggregate that initializes to the first value

Поиск
Список
Период
Сортировка
От Timothy Garnett
Тема Creating a non-strict custom aggregate that initializes to the first value
Дата
Msg-id CAPcyiQ2BuAc0438jbh5OnZDEgF=z4g8sdFbhLEy+MUpbqaxKWA@mail.gmail.com
обсуждение исходный текст
Ответы Re: Creating a non-strict custom aggregate that initializes to the first value  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: Creating a non-strict custom aggregate that initializes to the first value  (Francisco Olarte <folarte@peoplecall.com>)
Список pgsql-general
Hi all,

I'm trying to create a custom aggregate function that returns the value from the first row (possibly null).

For ex.

Table t
a | b
-----
1 | A
2 | NULL


SELECT my_first(b order by a) => A
SELECT my_first(b order by a DESC) => NULL

The straightforward way would seem to be something like

CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement )
RETURNS anyelement LANGUAGE sql IMMUTABLE STRICT AS $$
        SELECT $1;
$$;

but if that is declared strict then it would take the first non-null value and return A in my second example, if declared non-strict then the initial state would be fed as null rather then the first value. Is there a way to declare the function non-strict (so that null values are passed) but still have it initialize to the first value like it would if it was strict?


Thanks for the help,
Tim

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

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Would like to know how analyze works technically
Следующее
От: Anil Menon
Дата:
Сообщение: Strange behavior of insert CTE with trigger