How to use subqueries in SQLAlchemy to produce a moving average?

后端 未结 1 1922
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 17:07

My problem is that I want to retrieve both a list of measurements along with a moving average of those measurements. I can do that with this SQL statement (postgresql interval

相关标签:
1条回答
  • 2021-02-06 17:32

    Right, apparently what I needed was the use of a so-called scalar select. With the use of those I get this python code, which actually works as I want it to (generates the equivalent SQL to that of the first in my question which was my goal):

    moving_average_days = # configurable value, defaulting to 5
    ndays = # configurable value, defaulting to 90
    t1 = Measurements.alias('t1') ######
    t2 = Measurements.alias('t2')
    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)
    

    This gives this SQL:

    SELECT t1.time, t1.value,
        (
            SELECT avg(t2.value) AS avg_1
            FROM measurements AS t2 
            WHERE t2.time BETWEEN t1.time - :time_1 AND t1.time
        ) AS moving_average 
    FROM measurements AS t1
    WHERE t1.time > :time_2 ORDER BY t1.time;
    
    0 讨论(0)
提交回复
热议问题