[英]MySQL query to get array of averages of each month's data
I have a table named historical_currencies_rate
where I have saved last 8 years currency rate of some known currencies. 我有一个名为historical_currencies_rate
的表,在其中保存了过去8年中某些已知货币的汇率。 eg (EUR-USD). 例如(EUR-USD)。 Table looks like this 桌子看起来像这样
+---------------------------+---------------+------------+
| 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 |
+---------------------------+---- ----------+------------+
here I have shown you tha data of few currency upto 8 days and for year 2006 but i have a huge data of 18 currencies with 30 days data upto 2013. 在这里,我向您显示了直到2006年的8天之内的几种货币的数据,但是我有一个庞大的数据,其中包含了截至2013年的30天的18种货币的数据。
My query is that I want an array which contains the avg currency rate of a particular currency on monthly basis. 我的查询是我想要一个包含每月特定货币的平均货币汇率的数组。 for eg avg of EUR-USD
in my example for the month of January
is 1.19935
, JPY-USD
is 0.01275
, GBP-USD
is 1.559
and avg of EUR-USD
for the month of February
is 1.2030375
etc. 对于如平均EUR-USD
在我的例子为当月January
为1.19935
, JPY-USD
是0.01275
, GBP-USD
为1.559
和平均EUR-USD
的月February
是1.2030375
等。
I want array of averages of EUR-USD
or may be some other currency pair, of each month ie. 我想要每个月的EUR-USD
平均数组,也可以是其他一些货币对,即。 it should be like 它应该像
`[1.19935, 1.2030375, ......]`
query-result = [avg. 查询结果= [平均 of EUR-USD
for January, 2006
, avg. January, 2006
的EUR-USD
汇率, of EUR-USD
for February, 2006
, ............., avg. February, 2006
的EUR-USD
汇率,.............,平均 of EUR-USD
for January, 2013
, ..... ] and so on up to 8 years. January, 2013
的EUR-USD
January, 2013
,.....]等,最长8年。
Please help me in writing this in single query.Thanks 请帮助我在单个查询中编写此代码。谢谢
Take parts of date you want (in your case year and month) and group result on this base and currency: 取一部分日期(以您的年份和月份为例),并根据此基准和货币对结果进行分组:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.