Re: First Aggregate Funtion?

From: sudalai <sudalait2(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: First Aggregate Funtion?
Date: 2015-07-14 13:23:25
Message-ID: 1436880205628-5857866.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

The above implementation of "first" aggregate returns the first non-NULL item
value.

To get *first row item value* for a column use the below implementation.

-- create a function that push at most two element on given array
-- push the first row value at second index of the array
CREATE OR REPLACE FUNCTION two_value_holder(anyarray, anyelement)
returns anyarray as $$
select case when array_length($1,1) < 2 then array_append($1,$2) else
$1 end ;
$$ language sql immutable;

-- create a function that returns second element of an array
CREATE OR replace FUNCTION second_element (ANYARRAY)
RETURNS ANYELEMENT AS $$ SELECT $1[2]; $$ LANGUAGE SQL;

-- create first aggregate function that return first_row item value
CREATE AGGREGATE first(anyelement)(
SFUNC=two_value_holder,
STYPE=ANYARRAY,
INITCOND='{NULL}',
FINALFUNC=second_element
);

I hope this work..
--
Sudalai

-----
sudalai
--
View this message in context: http://postgresql.nabble.com/First-Aggregate-Funtion-tp1943031p5857866.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2015-07-14 13:24:35 Re: pgsql: Retain comments on indexes and constraints at ALTER TABLE ... TY
Previous Message Tom Lane 2015-07-14 13:02:58 Re: pgsql: Retain comments on indexes and constraints at ALTER TABLE ... TY