0

I have a list of exchanges and there rate and the date, it is updated every day,

example ->

usd - 135 - 26/02/2013
Yen - 230 - 26/02/2013
GBP - 200 - 26/02/2013

usd - 136 - 25/02/2013
Yen - 230 - 25/02/2013
GBP - 205 - 25/02/2013

I have a query that group-con them up and the query is this ->

SELECT Curr, Src, GROUP_CONCAT(Rate, '|', Feeddate 
ORDER BY feeddate ASC SEPARATOR ',' ) AS RateCal 
FROM rates 

so it looks like this ->

usd - 135 | 26/02/2013, 136 | 25/02/2013
Yen - 230 | 26/02/2013, 230 | 25/02/2013
GBP - 200 | 26/02/2013, 206 | 25/02/2013

But I also have a query to get me averages of each exchanges for each month which is this ->

SELECT  Curr, DATE_FORMAT(Feeddate,'%Y-%M') AS 'Year', AVG(`Rate`) as 'Rate'
FROM rates,

but what i would like to have now is to have the monthly averages of each exchange group concat like above

3
  • What do you like to have?
    – Arion
    Feb 27, 2013 at 13:35
  • but what i would like to have now is to have the monthly averages of each exchange group concat like above Feb 27, 2013 at 13:36
  • can you show your desired result?
    – John Woo
    Feb 27, 2013 at 13:45

1 Answer 1

0

Could it be this that you are looking for? Using your sample data and adding some for month of March.

SQL DEMO

select curr, monthname(date), avg(rate)
from rates
group by curr, monthname(Date);

| CURR | MONTHNAME(DATE) | AVG(RATE) |
--------------------------------------
|  GBP |        February |     202.5 |
|  GBP |           March |       207 |
|  usd |        February |     135.5 |
|  usd |           March |       136 |
|  Yen |        February |       230 |
|  Yen |           March |     232.5 |

It looks too simple to be true. So please comment.

AS per OP's comment:

SQL DEMO 2

select x.curr, 
GROUP_CONCAT(x.avgr, '|', x.mondate) as Months_Averages
from (select curr, monthname(date) mondate,
avg(rate) avgr
from rates
group by curr, monthname(date)
)x
group by x.curr;

| CURR |                      MONTHS_AVERAGES  |
------------------------------------------------
|  GBP |    202.5000| February,207.0000| March |
|  usd |    135.5000| February,136.0000| March |
|  Yen |    230.0000| February,232.5000| March |
5
  • If you want rates to be group_concat by exchange, then you need to show us how Exchange data is in the main table... :)
    – bonCodigo
    Feb 27, 2013 at 13:52
  • What I wanted was to some how connect my 2 queries, so it sould look like this GBP - 205|February, 204|January, ..., so what I get is GBP averages for each month and then groupconcat it, so i will have value for each month. Feb 27, 2013 at 13:54
  • @user2115506 please take a look at the updated code. That seems to match what you need, as it group_concats each month, and each month's average for each currency.
    – bonCodigo
    Feb 27, 2013 at 14:05
  • Sweet, that works the way I want, thank you very much, I been trying many methods, and i am still new to this. Feb 27, 2013 at 14:21
  • Glad it helped. If your question is answered, then you may accept this as the answer. As it helps community as well. No pressure, please see how to accept an answer. Thanks.
    – bonCodigo
    Feb 28, 2013 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.