query = select([t1.c.time, t1.c.value, select([func.avg(t2.c.value)], t2.c.
    time.between(t1.c.time - datetime.timedelta(moving_average_days), t1.c.
    time)).label('moving_average')], t1.c.time > datetime.datetime.utcnow() -
    datetime.timedelta(ndays)).order_by(t1.c.time)
How to use subqueries in SQLAlchemy to produce a moving average?