所有物品的5天移动平均值

时间:2012-06-03 11:45:22

标签: mysql average

下面是我的MYSQL表结构

ticker
_date
open
high
low
close
volume

下面是样本数据

ALMOND-I,2012-03-27,373.5000,395.0000,373.5000,380.2500,10
ALMOND-I,2012-03-31,391.0000,391.0000,386.0000,387.0000,5
ALMOND-I,2012-04-03,378.5000,386.0000,364.5000,378.5000,14
ALMOND-I,2012-04-11,380.0000,389.5000,370.7500,380.0000,3
ALMOND-I,2012-04-12,391.0000,391.0000,370.2500,380.7500,2
ALMOND-I,2012-04-16,392.0000,392.0000,392.0000,392.0000,1
ALMOND-I,2012-04-18,380.2500,380.2500,380.2500,380.2500,1
ALMOND-I,2012-05-02,371.0000,393.5000,371.0000,381.5000,6
ALMOND-I,2012-05-15,393.0000,393.0000,393.0000,393.0000,1
ALMOND-I,2012-05-21,416.7500,417.0000,393.0000,398.5000,10
ALMOND-I,2012-05-22,410.5000,410.5000,410.5000,410.5000,1
ALMOND-I,2012-05-30,435.2500,435.2500,435.2500,435.2500,1
ALUMINIUM-I,2012-05-22,110.8000,110.9500,110.2500,110.7000,4939
ALUMINIUM-I,2012-05-23,110.9000,111.4500,110.7500,111.2500,6996
ALUMINIUM-I,2012-05-24,111.0000,111.4500,109.9000,110.4500,7111
ALUMINIUM-I,2012-05-25,110.4500,110.6000,109.4000,109.7000,5020
ALUMINIUM-I,2012-05-26,109.6000,109.8500,109.6000,109.8000,116
ALUMINIUM-I,2012-05-28,109.4000,110.3000,109.3500,110.1500,2614
ALUMINIUM-I,2012-05-29,110.4500,111.0500,110.3000,110.8500,3956
ALUMINIUM-I,2012-05-30,110.9000,111.0500,110.1000,110.9500,5208
ALUMINIUM-I,2012-05-31,110.9500,111.7500,110.7500,111.3500,3553
ALUMINIUM-I,2012-06-01,111.1500,111.3000,108.7000,108.8500,7532
ALUMINIUM-I,2012-06-02,108.8000,108.8000,108.1000,108.2500,555
GOLD-I,2012-05-21,29018.0000,29174.0000,28956.0000,29035.0000,33330
GOLD-I,2012-05-22,29068.0000,29190.0000,28827.0000,29087.0000,45281
GOLD-I,2012-05-23,29026.0000,29084.0000,28831.0000,28969.0000,50523
GOLD-I,2012-05-24,29020.0000,29102.0000,28854.0000,28880.0000,45170
GOLD-I,2012-05-25,28841.0000,28937.0000,28748.0000,28902.0000,32176
GOLD-I,2012-05-26,28964.0000,28964.0000,28918.0000,28925.0000,1427
GOLD-I,2012-05-28,28901.0000,29055.0000,28882.0000,28965.0000,17158
GOLD-I,2012-05-29,28994.0000,29135.0000,28819.0000,28859.0000,39300
GOLD-I,2012-05-30,28845.0000,29154.0000,28755.0000,29117.0000,38733
GOLD-I,2012-05-31,29120.0000,29198.0000,29005.0000,29148.0000,22810
GOLD-I,2012-06-01,29130.0000,29871.0000,28765.0000,29724.0000,4753
GOLD-I,2012-06-02,29745.0000,29929.0000,29740.0000,29866.0000,337
SILVER-I,2012-05-21,54525.0000,54805.0000,53865.0000,54130.0000,66128
SILVER-I,2012-05-22,54187.0000,55119.0000,53873.0000,54392.0000,78147
SILVER-I,2012-05-23,54250.0000,54250.0000,53320.0000,53818.0000,87066
SILVER-I,2012-05-24,53999.0000,54785.0000,53980.0000,54317.0000,74940
SILVER-I,2012-05-25,54250.0000,54500.0000,53882.0000,54426.0000,61457
SILVER-I,2012-05-26,54481.0000,54535.0000,54465.0000,54479.0000,1535
SILVER-I,2012-05-28,54533.0000,54867.0000,54302.0000,54351.0000,33566
SILVER-I,2012-05-29,54448.0000,54885.0000,53790.0000,53964.0000,82448
SILVER-I,2012-05-30,53900.0000,54578.0000,53560.0000,54483.0000,87322
SILVER-I,2012-05-31,54377.0000,54465.0000,53820.0000,54110.0000,72225
SILVER-I,2012-06-01,54002.0000,54682.0000,52813.0000,54445.0000,107052
SILVER-I,2012-06-02,54530.0000,54610.0000,54451.0000,54487.0000,2371
WHEAT-I,2010-07-22,1276.9000,1276.9000,1228.2000,1247.9000,3
WHEAT-I,2010-07-23,1258.0000,1258.0000,1258.0000,1258.0000,2
WHEAT-I,2010-07-29,1245.0000,1245.0000,1245.0000,1245.0000,1
WHEAT-I,2010-07-30,1235.0000,1235.0000,1235.0000,1235.0000,1
WHEAT-I,2010-08-02,1245.0000,1245.0000,1240.0000,1242.5000,2
WHEAT-I,2010-08-04,1220.0000,1220.0000,1220.0000,1220.0000,1
WHEAT-I,2010-08-05,1240.0000,1240.0000,1232.0000,1237.3000,3
WHEAT-I,2011-03-19,1218.9000,1218.9000,1218.9000,1218.9000,1
WHEAT-I,2011-03-21,1182.3000,1182.3000,1182.3000,1182.3000,1
WHEAT-I,2011-03-25,1151.5000,1151.5000,1151.5000,1151.5000,2
WHEAT-I,2011-04-19,1160.0000,1160.0000,1149.5000,1152.3000,4
WHEAT-I,2011-04-28,1170.0000,1195.0000,1170.0000,1182.5000,4
ZINC-I,2012-05-21,103.8000,104.7500,103.8000,104.7000,13197
ZINC-I,2012-05-22,104.7000,105.6000,104.3000,105.5000,14996
ZINC-I,2012-05-23,105.3500,105.4500,104.5000,105.0000,16656
ZINC-I,2012-05-24,104.8000,105.3000,104.2000,104.6500,12688
ZINC-I,2012-05-25,104.6000,105.3000,104.1500,104.9500,12523
ZINC-I,2012-05-26,104.9000,105.0000,104.8000,104.9500,215
ZINC-I,2012-05-28,105.0000,105.5500,104.9000,105.2500,7387
ZINC-I,2012-05-29,105.6000,106.7000,105.4000,106.6000,14092
ZINC-I,2012-05-30,106.3000,106.5000,105.4500,106.3000,11983
ZINC-I,2012-05-31,106.0500,107.4000,105.7500,107.1500,5936
ZINC-I,2012-06-01,105.6500,105.7500,103.7000,104.6500,15231
ZINC-I,2012-06-02,104.5500,104.6500,104.2500,104.4000,551

我想计算最近5个日期的每件商品的移动平均值CLOSE 2012-06-02锌平均收市价的例子是(104.4000 + 104.6500 + 107.1500 + 106.3000 + 106.6000)/5=105.82

我写了一个查询

SELECT x.ticker , AVG( x.close), x._date
FROM 
(
    SELECT t.ticker , t.close,t._date
    FROM mcx_eod_data t
    ORDER BY t._date DESC
) x
GROUP BY x.ticker

它给了我平均值,我认为从特定股票代码的开始到结束的整个收盘

所以我做了一些研究并添加了限制声明并修改了我的查询

SELECT x.ticker , AVG( x.close), x._date
FROM 
(
    SELECT t.ticker , t.close,t._date
    FROM mcx_eod_data t
    ORDER BY t._date DESC limit 5
) x
GROUP BY x.ticker

但现在只提取5个代码的数据

我希望所有代码

平均值 平均值

1 个答案:

答案 0 :(得分:0)

用where子句替换顺序和限制:

WHERE t._date >= DATE_SUB(NOW(), INTERVAL 5 DAY)