This is pretty cool, I didn't know about window functions in Postgres. Most of the time when dealing with time series data I end up dropping into Python (pandas) or R (xts package), but it is nice to see how to do a lot of analysis right in the database.