Re: First Aggregate Funtion? - Mailing list pgsql-hackers

From Mike Rylander
Subject Re: First Aggregate Funtion?
Date
Msg-id b918cf3d0603311735v5dde21d4pfcd613c09d80cf57@mail.gmail.com
Whole thread Raw
In response to Re: First Aggregate Funtion?  (Martijn van Oosterhout <kleptog@svana.org>)
Responses Re: First Aggregate Funtion?  (sudalai <sudalait2@gmail.com>)
List pgsql-hackers
On 3/31/06, Martijn van Oosterhout <kleptog@svana.org> wrote:
> On Fri, Mar 31, 2006 at 03:02:47PM -0600, Tony Caduto wrote:
> > Has there ever been any talk of adding a first aggregate function?
> > It would make porting from Oracle and Access much easier.
> >
> > Or is there something in the contrib modules that I might have missed?
>
> There are several oracle compatability modules:
>
> http://pgfoundry.org/projects/oracompat/
> http://pgfoundry.org/projects/orafce/
>
> I'm sure there's many more if you look...

If all you want is FIRST() and LAST() then:

-- 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
);


Hope that helps!

--
Mike Rylander
mrylander@gmail.com
GPLS -- PINES Development
Database Developer
http://open-ils.org


pgsql-hackers by date:

Previous
From: David Fetter
Date:
Subject: Re: Suggestion: Which Binary?
Next
From: Neil Conway
Date:
Subject: Re: listen not schema-aware