0

so in MySQL I have a table like:

date account amount
2021-01 1 3000
2021-01 2 2100
2021-01 3 1800
2021-02 1 3500
2021-02 2 1500
2021-02 3 2500

since it has 3 different accounts on 2 different date, I want to group it by date and account then count average so the table would be like:

date account1 account2 account3 average_amount
2021-01 3000 2100 1800 2300
2021-02 3500 1500 2200 2400

what do you think the best query to achieve the result to convert account rows into columns in MySQL?

irwanian
  • 1
  • 1
  • What have you tried? Questions about pivoting in MySQL have been answered many times on Stack Overflow. For example: https://stackoverflow.com/questions/25594902/row-to-column-transformation-in-mysql/25594969#25594969 – Bill Karwin Jun 21 '21 at 13:50
  • First post on stack overflow, actually at first i don't even know if this is called pivot, i searched like 'how to turn rows into columns on MySQL' but now i know if it's called pivot and glad to find so much answers related to my question. thank You!! – irwanian Jun 21 '21 at 14:03

0 Answers0