Postgresql Moving Average Window Function Javascript Code