【发布时间】:2012-07-19 23:36:54
【问题描述】:
我有一个具有以下架构的表:
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()?
【问题讨论】:
-
定义顺序。什么是“以前的”?
标签: postgresql stored-procedures plpgsql window-functions