MySQL查询以获取每个月的平均数据数据

时间:2014-06-06 07:01:47

标签: mysql sql arrays

我有一张名为historical_currencies_rate的表,我保存了一些已知货币的最近8年货币汇率。例如(欧元兑美元)。表看起来像这样

+---------------------------+---------------+------------+
|       rate_date           | currency_pair |    rate    |
+---------------------------+---------------+------------+
|       2006-01-01          |    EUR-USD    |  1.1797    |
|       2006-02-01          |    EUR-USD    |  1.1826    |
|       2006-03-01          |    EUR-USD    |  1.1875    |
|       2006-04-01          |    EUR-USD    |  1.2083    |
|       2006-05-01          |    EUR-USD    |  1.2088    |
|       2006-06-01          |    EUR-USD    |  1.2093    |
|       2006-07-01          |    EUR-USD    |  1.2093    |
|       2006-08-01          |    EUR-USD    |  1.2093    |
|       2006-01-01          |    JPY-USD    |  0.01275   |
|       2006-02-01          |    JPY-USD    |  0.01275   |
|       2006-03-01          |    JPY-USD    |  0.01275   |
|       2006-04-01          |    JPY-USD    |  0.01275   |
|       2006-05-01          |    JPY-USD    |  0.01275   |
|       2006-06-01          |    JPY-USD    |  0.01275   |
|       2006-07-01          |    JPY-USD    |  0.01275   |
|       2006-08-01          |    JPY-USD    |  0.01275   |
|       2006-01-01          |    GBP-USD    |  1.559     |
|       2006-02-01          |    GBP-USD    |  1.559     |
|       2006-03-01          |    GBP-USD    |  1.559     |
|       2006-04-01          |    GBP-USD    |  1.559     |
|       2006-05-01          |    GBP-USD    |  1.559     |
|       2006-06-01          |    GBP-USD    |  1.559     |
|       2006-07-01          |    GBP-USD    |  1.559     |
|       2006-08-01          |    GBP-USD    |  1.559     |
|       2006-01-01          |    AUD-USD    |  1.0515    |
|       2006-02-01          |    AUD-USD    |  1.0515    |
|       2006-03-01          |    AUD-USD    |  1.0515    |
|       2006-04-01          |    AUD-USD    |  1.0515    |
|       2006-05-01          |    AUD-USD    |  1.0515    |
|       2006-06-01          |    AUD-USD    |  1.0515    |
|       2006-07-01          |    AUD-USD    |  1.0515    |
|       2006-08-01          |    AUD-USD    |  1.0515    |
|       2006-01-02          |    EUR-USD    |  1.2092    |
|       2006-02-02          |    EUR-USD    |  1.2066    |
|       2006-03-02          |    EUR-USD    |  1.2061    |
|       2006-04-02          |    EUR-USD    |  1.2061    |
|       2006-05-02          |    EUR-USD    |  1.2061    |
|       2006-06-02          |    EUR-USD    |  1.1981    |
|       2006-07-02          |    EUR-USD    |  1.1973    |
|       2006-08-02          |    EUR-USD    |  1.1948    |
+---------------------------+---- ----------+------------+

在这里,我向您展示了8天和2006年的几种货币的数据,但我拥有18种货币的大量数据,截至2013年的数据为30天。

我的查询是,我想要一个包含每月特定货币的平均货币汇率的数组。例如,EUR-USD January 1.19935 JPY-USD 0.01275GBP-USD1.559EUR-USDFebruary1.2030375 } EUR-USD `[1.19935, 1.2030375, ......]` 的{​​{1}}平均为EUR-USD等。

我希望每个月的平均值January, 2006或可能是其他一些货币对,即。它应该像

EUR-USD

query-result = [avg。 February, 2006的{​​{1}},平均值EUR-USD的{​​{1}},.............,平均。 January, 2013,{{1}},.....等等,最长可达8年。

请在单个查询中帮我写这篇文章。谢谢

1 个答案:

答案 0 :(得分:1)

获取您想要的部分日期(在您的情况下为年份和月份),并在此基础和货币上分组结果:

SELECT DATE_FORMAT(rate_date,'%M, %Y') AS rate_month,currency_pair,AVG(rate) AS avg_rate 
FROM historical_currencies_rate
WHERE currency_pair='EUR-USD'
GROUP BY DATE_FORMAT(rate_date,'%M, %Y'),currency_pair
ORDER BY rate_month