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

Reply via email to