【发布时间】:2021-01-18 05:26:31
【问题描述】:
正在尝试验证我的移动平均计算...但是我得到了两个不同的值,我希望每个查询都返回相同的值:
select sum(a.processing) / 50 as myMean
from (select created, processing
from myTable
where name = 'stack'
order by created desc
limit 50) a
union
select b.*
from (select AVG(processing)
filter (where name = 'stack')
OVER (ORDER BY created desc ROWS BETWEEN 49 PRECEDING AND CURRENT ROW)
from myTable
where name = 'stack'
order by created desc
limit 1) b
第一个查询我得到处理值的总和,然后除以 N(这里是 50),第二个查询我尝试使用窗口函数来实现同样的事情 - 获得最后 50 个的平均处理值name = stack 的行。
【问题讨论】:
-
有什么问题?这似乎完全符合你的描述?使用一些示例数据并期望输出会更容易理解您想要什么。
-
@T.Peter 我得到两个不同的值,我希望这些值是相同的
标签: sql postgresql