Skip to content

Instantly share code, notes, and snippets.

@bitdivine
Last active June 3, 2016 09:04
Show Gist options
  • Select an option

  • Save bitdivine/49033ae7b7a92fe7ea0bc592fd325774 to your computer and use it in GitHub Desktop.

Select an option

Save bitdivine/49033ae7b7a92fe7ea0bc592fd325774 to your computer and use it in GitHub Desktop.
Postgres aggregator to get the first non-null value.
-- Like the Postgres first_val() aggregator, exceppt that this returns the first non-null value.
CREATE OR REPLACE FUNCTION first_non_null_fun(numeric, numeric) RETURNS numeric
AS 'SELECT case when $1 is null then $2 else $1 end;'
LANGUAGE SQL IMMUTABLE STRICT;
CREATE AGGREGATE first_non_null ( numeric ) (
SFUNC = first_non_null_fun,
STYPE = numeric
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment