PostgreSQL中是否实现了数据插补方法最后一次观察结果(LOCF)?如果没有,我如何实现这个方法?

最佳答案

以下代码假设有一个表tbl,其中包含ab(键)、t(时间)和v(locf估算值):

create or replace function locf_s(a float, b float)
returns float
language sql
as '
  select coalesce(b, a)
';

drop aggregate if exists locf(float);
CREATE AGGREGATE locf(FLOAT) (
  SFUNC = locf_s,
  STYPE = FLOAT
);

select a,b,t,v,
    locf(v) over (PARTITION by a,b ORDER by t) as v_locf
from tbl
order by a,b,t
;

SQLFiddle
教程:"LOCF and Linear Imputation with PostgreSQL"

关于postgresql - 是否在PostgreSQL中实现了上次观察结转(LOCF)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27387300/

10-11 17:34