我有一个具有以下架构的表:
CREATE TABLE foo (
the_date date,
user_id int,
pop REAL,
hip REAL,
lop REAL,
cop REAL
);
我想编写一个foofunc()
引用前一行并根据以下逻辑返回值的函数:
function foofunc() returns numeric as $body$
begin
-- # calculate (current_row.hip - current_row.lop) as val1 for current row
-- # calculate abs(current_row.hip - previous_row.cop) as val2
-- # calculate abs(current_row.lop - previous_row.cop) as val3
-- RETURN max(val1, val2, val3)
end;
$body$ language plpgsql
我希望能够foofunc()
像这样调用函数:
SELECT foofunc()
from foo
where the_date between date1 and date2
and user_id=some_id;
我该如何实施foofunc()
?