问题
I'm trying to write a window function to calculate exponential smoothing in Redshift. I am referencing this post (here).
SELECT p.*,
(sum(power((1/0.8666666), seqnum) * price) over (order by seqnum rows unbounded preceding) +
first_value(price) over (order by seqnum rows unbounded preceding)
) / power((1/.13333333), seqnum+1 )
FROM (SELECT
date,
row_number() over (order by date) - 1 as seqnum,
price
FROM table.prices
) p;
The issue is that when the value of the smoothing constant is anything but .5 (which is a 3-day period given by K = Smoothing Constant = 2 / (1 + n)), the equation doesn't compute the values correctly. However, when the values are .5 (so, replace .866666 and .13333 each with .5), it works perfectly as checked in Excel: X = (K * (C - P)) + P. (see this link).
Some notes: Redshift can't do recursive CTEs, so it has to be a Window Function. Normally, I would use R or Python to do this, but for this task it needs to be computed in SQL. Any and all ideas are welcome. Thank you!
来源:https://stackoverflow.com/questions/60982644/exponential-smoothing-in-redshift