Entries for label: postgresql

Found 2 entries.

PostgreSQL Aggregates

first() and last()

From: http://archives.postgresql.org/pgsql-hackers/2006-03/msg01324.php

-- Create a function that always returns the first non-NULL item
CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement )
RETURNS anyelement AS $$
        SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
$$ LANGUAGE SQL STABLE;
--
-- And then wrap an aggreagate around it
CREATE AGGREGATE public.first (
        sfunc    = public.first_agg,
        basetype = anyelement,
        stype    = anyelement
);
--
-- Create a function that always returns the last non-NULL item
CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement )
RETURNS anyelement AS $$
        SELECT $2;
$$ LANGUAGE SQL STABLE;
--
-- And then wrap an aggreagate around it
CREATE AGGREGATE public.last (
        sfunc    = public.last_agg,
        basetype = anyelement,
        stype    = anyelement
);

(Ends)

Labels: postgresql

Inserted: 2010-01-29 03:36 (2 years ago)

PostgeSQL

Some useful PostgreSQL things I have come across:

(Ends)

Labels: postgresql

Inserted: 2010-01-29 03:33 (2 years ago)