我想找到过去30天的移动平均线。例如
今天的日期是17/11/15,我的数据只有几天
原始数据
date qty
06/11/15 5
08/11/15 7
09/11/15 8
10/11/15 12
11/11/15 34
15/11/15 45
16/11/15 12
17/11/15 7
查找30天移动平均数据 - 需要获得movingAvg,如下表-movingAvg列
date qty movingAvg
17/10/15 0 0
18/10/15 0 0
19/10/15 0 0
20/10/15 0 0
21/10/15 0 0
22/10/15 0 0
23/10/15 0 0
24/10/15 0 0
25/10/15 0 0
26/10/15 0 0
27/10/15 0 0
28/10/15 0 0
29/10/15 0 0
30/10/15 0 0
31/10/15 0 0
01/11/15 0 0
02/11/15 0 0
03/11/15 0 0
04/11/15 0 0
05/11/15 0 0
06/11/15 5 0.14
07/11/15 0 1.4
08/11/15 7 0.4
09/11/15 8 0.67
10/11/15 12 1.06
11/11/15 34 2.2
12/11/15 0 2.2
13/11/15 0 2.2
14/11/15 0 2.2
15/11/15 45 3.7
16/11/15 12 4.1
17/11/15 7 4.33
请帮我解决这个问题,或者是否有替代方案。
答案 0 :(得分:0)
使用MySQL str_to_date
或INTERVAL
select date,qty,avg(qty) from table
WHERE str_to_date(date,'%d/%m/%y') >= DATE(NOW()) - INTERVAL 30 DAY
GROUP BY date
答案 1 :(得分:0)
有很多方法可以给这只猫上皮。这是一个使用日历表的所有合理日期(一个令人沮丧的小数据集)...
window.addEventListener("message", function(event) {
if (event.data.type == "hideFrame") {
$("#myFrame").hide();
}
});
答案 2 :(得分:0)
生成一系列日期,并将其与原始数据相结合。加入两次,一次获得数天,一次获得过去30天的平均值。
SELECT sub0.aDate, IFNULL(od1.qty, 0), AVG(IFNULL(od2.qty, 0))
FROM
(
SELECT DATE_SUB( CURRENT_DATE(), INTERVAL (units.a + tens.a * 10) DAY) AS aDate, DATE_SUB( CURRENT_DATE(), INTERVAL ((units.a + tens.a * 10) + 29) DAY) AS prevDate
FROM
(SELECT 0 a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN
(SELECT 0 a UNION SELECT 1 UNION SELECT 2) tens
) sub0
INNER JOIN
(
SELECT DATE_SUB( CURRENT_DATE(), INTERVAL (units.a + tens.a * 10) DAY) AS aDate
FROM
(SELECT 0 a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN
(SELECT 0 a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) tens
) sub1
ON sub1.aDate BETWEEN sub0.prevDate AND sub0.aDate
LEFT OUTER JOIN original_data od1 ON od1.`date` = sub0.aDate
LEFT OUTER JOIN original_data od2 ON od2.`date` = sub1.aDate
GROUP BY aDate, od1.qty
ORDER BY aDate
答案 3 :(得分:-2)
试试这个......
SELECT * FROM your_table
WHERE date >= DATEADD(day,-30, now()) AND date <= now()
希望这会有所帮助..