Second maximum value

From: Marco Bubke <marco(at)bubke(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: Second maximum value
Date: 2003-04-27 10:37:53
Message-ID: 3EABB301.5040000@bubke.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello

I'm new to postgres and I have tried to write a function which returns
the second(third...) maximum to use it in a aggregation. But it's not
function and I have no clue whats wrong.

Here what I do:

create type counter_max as (
maximum real,
value int );

create function get_counter_max(real, int) returns counter_max as
'select $1, $2' language sql;

create or replace function x_max(counter_max, real) returns counter_max as '
declare
maxi alias for $1;
value alias for $2;
result counter_max;
begin
if maxi.counter = 0 then
return maxi;
elsif value >= maxi.maximum then
result.maximum := value;
result.counter := maxi.counter -1;
return result;
else
return maxi;
end if;
end;
' language 'plpgsql';

When I execute: select x_max(get_counter_max(1., 1), 2.::real);
it says:

WARNING: plpgsql: ERROR during compile of x_max near line 11
ERROR: return type mismatch in function returning tuple at or near "result"

Thanks in advance

Marco

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2003-04-27 16:12:05 Re: Second maximum value
Previous Message David W Noon 2003-04-27 10:30:13 Re: EASY QUESTION!!