MySQL计算N行的移动平均值 [英] MySQL calculate moving average of N rows
问题描述
我正在尝试计算N个行的移动平均值 ,对于单个查询中的所有行 .在示例情况下,我尝试计算50行的移动平均值.
I'm trying to calculate the moving average of N rows, for all rows in a single query. In the example case, I am attempting to calculate the moving average of 50 rows.
SELECT
h1.date,
h1.security_id,
( SELECT
AVG(last50.close)
FROM (
SELECT h.close
FROM history as h
WHERE h.date <= h1.date AND h.security_id = h1.security_id
ORDER BY h.date DESC
LIMIT 50
) as last50
) as avg50
FROM history as h1
但是,MySQL在运行此查询时给我一个错误:
However, MySQL gives me an error when running this query:
Unknown column 'h1.date' in 'where clause'
我正在尝试此方法,因为列出的其他解决方案似乎并不适合我的用例.对于N个天的移动平均值有一些解决方案,但是由于我的数据集中没有考虑所有日期,因此我需要N个行的平均值.
I'm trying this method because the other solutions listed don't really seem to work for my use case. There are solutions for a moving average of N days, but since all dates are not accounted for in my data set, I need the average of N rows.
此解决方案(如下所示)没有之所以起作用,是因为AVG
(也包括SUM
和COUNT
)不能解释LIMIT
:
This solution, shown below, doesn't work because AVG
(also SUM
and COUNT
) doesn't account for LIMIT
:
SELECT
t1.data_date
( SELECT SUM(t2.price) / COUNT(t2.price)
FROM t as t2
WHERE t2.data_date <= t1.data_date
ORDER BY t2.data_date DESC
LIMIT 5
) AS 'five_row_moving_average_price'
FROM t AS t1
ORDER BY t1.data_date;
此问题看起来很有前途,但对我来说有点难以理解.
This question looks promising, but is somewhat indecipherable to me.
有什么建议吗? 这里是一个SQLFiddle .
推荐答案
计划
- 过去50天的自我加入历史
- 按日期和安全ID(当前)进行平均分组
- self join history on last 50 days
- take average grouping by date and security id ( of current )
查询
select curr.date, curr.security_id, avg(prev.close)
from history curr
inner join history prev
on prev.`date` between date_sub(curr.`date`, interval 49 day) and curr.`date`
and curr.security_id = prev.security_id
group by 1, 2
order by 2, 1
;
输出
+---------------------------+-------------+--------------------+
| date | security_id | avg(prev.close) |
+---------------------------+-------------+--------------------+
| January, 04 2016 00:00:00 | 1 | 10.770000457763672 |
| January, 05 2016 00:00:00 | 1 | 10.800000190734863 |
| January, 06 2016 00:00:00 | 1 | 10.673333485921225 |
| January, 07 2016 00:00:00 | 1 | 10.59250020980835 |
| January, 08 2016 00:00:00 | 1 | 10.432000160217285 |
| January, 11 2016 00:00:00 | 1 | 10.40166680018107 |
| January, 12 2016 00:00:00 | 1 | 10.344285828726631 |
| January, 13 2016 00:00:00 | 1 | 10.297500133514404 |
| January, 14 2016 00:00:00 | 1 | 10.2877779006958 |
| January, 04 2016 00:00:00 | 2 | 56.15999984741211 |
| January, 05 2016 00:00:00 | 2 | 56.18499946594238 |
| .. | .. | .. |
+---------------------------+-------------+--------------------+
参考
已修改为使用最后50行
select
rnk_curr.`date`, rnk_curr.security_id, avg(rnk_prev50.close)
from
(
select `date`, security_id,
@row_num := if(@lag = security_id, @row_num + 1,
if(@lag := security_id, 1, 1)) as row_num
from history
cross join ( select @row_num := 1, @lag := null ) params
order by security_id, `date`
) rnk_curr
inner join
(
select date, security_id, close,
@row_num := if(@lag = security_id, @row_num + 1,
if(@lag := security_id, 1, 1)) as row_num
from history
cross join ( select @row_num := 1, @lag := null ) params
order by security_id, `date`
) rnk_prev50
on rnk_curr.security_id = rnk_prev50.security_id
and rnk_prev50.row_num between rnk_curr.row_num - 49 and rnk_curr.row_num
group by 1,2
order by 2,1
;
注释
if函数将强制对变量求值的正确顺序.
the if function is to force the correct order of evaluation of variables.
这篇关于MySQL计算N行的移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!