MGB C
MGB C

Reputation: 452

convert row table to column in mysql

I have a column chart that the user can insert a value in every year.

my table is look like this.

enter image description here

is there any possibility to view my table like this?

  |  Course   | 2013 | 2014 | 2015 |  // column name
  |  BSIT     | 657  | 453  | 424  |
  |  Education| 657  | 453  | 424  |

I want to view like that so that my column chart display look like this

enter image description here

I don't know if this question is possible to do. I wish...

Upvotes: 0

Views: 29

Answers (1)

user5992977
user5992977

Reputation:

If its limited to a small amount of years(not something like then 20 previous years) then it can be achieved with conditional aggregation like this:

SELECT Course,
       max(CASE WHEN year = 2013 then value end) as year_2013,
       max(CASE WHEN year = 2014 then value end) as year_2014,
       max(CASE WHEN year = 2015 then value end) as year_2015,
       .....More years if you need
FROM YourTable
GROUP BY Course

Upvotes: 2

Related Questions