<tfoot id='I15tm'></tfoot>

<legend id='I15tm'><style id='I15tm'><dir id='I15tm'><q id='I15tm'></q></dir></style></legend>

  • <small id='I15tm'></small><noframes id='I15tm'>

      <bdo id='I15tm'></bdo><ul id='I15tm'></ul>
    1. <i id='I15tm'><tr id='I15tm'><dt id='I15tm'><q id='I15tm'><span id='I15tm'><b id='I15tm'><form id='I15tm'><ins id='I15tm'></ins><ul id='I15tm'></ul><sub id='I15tm'></sub></form><legend id='I15tm'></legend><bdo id='I15tm'><pre id='I15tm'><center id='I15tm'></center></pre></bdo></b><th id='I15tm'></th></span></q></dt></tr></i><div id='I15tm'><tfoot id='I15tm'></tfoot><dl id='I15tm'><fieldset id='I15tm'></fieldset></dl></div>

      1. 如何在 SQLAlchemy 中使用子查询来生成移动平均线?

        How to use subqueries in SQLAlchemy to produce a moving average?(如何在 SQLAlchemy 中使用子查询来生成移动平均线?)
            <tbody id='my1Q5'></tbody>
            • <bdo id='my1Q5'></bdo><ul id='my1Q5'></ul>

              <small id='my1Q5'></small><noframes id='my1Q5'>

                <i id='my1Q5'><tr id='my1Q5'><dt id='my1Q5'><q id='my1Q5'><span id='my1Q5'><b id='my1Q5'><form id='my1Q5'><ins id='my1Q5'></ins><ul id='my1Q5'></ul><sub id='my1Q5'></sub></form><legend id='my1Q5'></legend><bdo id='my1Q5'><pre id='my1Q5'><center id='my1Q5'></center></pre></bdo></b><th id='my1Q5'></th></span></q></dt></tr></i><div id='my1Q5'><tfoot id='my1Q5'></tfoot><dl id='my1Q5'><fieldset id='my1Q5'></fieldset></dl></div>
                  <tfoot id='my1Q5'></tfoot><legend id='my1Q5'><style id='my1Q5'><dir id='my1Q5'><q id='my1Q5'></q></dir></style></legend>

                • 本文介绍了如何在 SQLAlchemy 中使用子查询来生成移动平均线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的问题是我想检索测量列表以及这些测量的移动平均值.我可以用这个 SQL 语句(postgresql 间隔语法)做到这一点:

                  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 syntax):

                  SELECT time, value,                
                     (
                         SELECT AVG(t2.value)
                         FROM measurements t2
                         WHERE t2.time BETWEEN t1.time - interval '5 days' AND t1.time
                     ) moving_average
                  FROM measurements t1
                  ORDER BY t1.time;
                  

                  我想让 SQLAlchemy 代码产生一个类似的语句来达到这个效果.我目前有这个 Python 代码:

                  I want to have the SQLAlchemy code to produce a similar statement to this effect. I currently have this Python code:

                  moving_average_days = # configureable value, defaulting to 5
                  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))],
                              t1.c.time > (datetime.datetime.utcnow() - datetime.timedelta(ndays))). 
                          order_by(Measurements.c.time)
                  

                  然而,生成这个 SQL:

                  That however, generates this SQL:

                  SELECT t1.time, t1.value, avg_1
                  FROM measurements AS t1,
                      (
                          SELECT avg(t2.value) AS avg_1
                          FROM measurements AS t2
                          WHERE t2.time BETWEEN t1.time - %(time_1)s AND t1.time
                      )
                  WHERE t1.time > %(time_2)s
                  ORDER BY t1.time;
                  

                  该 SQL 将子查询作为 FROM 子句的一部分,在该子句中它不能对顶级值的列值进行标量访问,即它会导致 PostgreSQL 吐出此错误:

                  That SQL has the subquery as part of the FROM clause where it cannot have scalar access to the column values of the top-level values, i.e. it causes PostgreSQL to spit out this error:

                  ERROR:  subquery in FROM cannot refer to other relations of same query level
                  LINE 6:         WHERE t2.time BETWEEN t1.time - interval '5 days' AN...
                  

                  因此我想知道的是:如何让 SQLAlchemy 将子查询移动到 SELECT 子句?

                  What I would thus like to know is: how do I get SQLAlchemy to move the subquery to the SELECT clause?

                  另一种获得移动平均线的方法(无需对每个(时间、值)对执行查询)也是一种选择.

                  Alternatively another way to get a moving average (without performing a query for each (time,value) pair) would be an option.

                  推荐答案

                  好吧,显然我需要的是使用所谓的 标量选择.通过使用这些,我得到了这个 python 代码,它实际上可以按照我的意愿工作(生成与我的目标中的第一个问题等效的 SQL):

                  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)
                  

                  这给出了这个 SQL:

                  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;
                  

                  这篇关于如何在 SQLAlchemy 中使用子查询来生成移动平均线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                    <tfoot id='hwUIy'></tfoot>

                        1. <i id='hwUIy'><tr id='hwUIy'><dt id='hwUIy'><q id='hwUIy'><span id='hwUIy'><b id='hwUIy'><form id='hwUIy'><ins id='hwUIy'></ins><ul id='hwUIy'></ul><sub id='hwUIy'></sub></form><legend id='hwUIy'></legend><bdo id='hwUIy'><pre id='hwUIy'><center id='hwUIy'></center></pre></bdo></b><th id='hwUIy'></th></span></q></dt></tr></i><div id='hwUIy'><tfoot id='hwUIy'></tfoot><dl id='hwUIy'><fieldset id='hwUIy'></fieldset></dl></div>
                          <legend id='hwUIy'><style id='hwUIy'><dir id='hwUIy'><q id='hwUIy'></q></dir></style></legend>

                          <small id='hwUIy'></small><noframes id='hwUIy'>

                            <tbody id='hwUIy'></tbody>
                          • <bdo id='hwUIy'></bdo><ul id='hwUIy'></ul>