Created
April 14, 2019 00:52
-
-
Save requaos/a5ffd522fa372b452773c756d7553ba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- From: https://stackoverflow.com/questions/8871426/how-to-calculate-an-exponential-moving-average-on-postgres | |
start transaction; | |
create or replace function ema_func(state numeric, inval numeric, alpha numeric) | |
returns numeric | |
language plpgsql as $$ | |
begin | |
return case | |
when state is null then inval | |
else alpha * inval + (1-alpha) * state | |
end; | |
end | |
$$; | |
drop aggregate if exists ema(numeric, numeric); | |
create aggregate ema(numeric, numeric) (sfunc = ema_func, stype = numeric); | |
commit transaction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment