DeadEnd
DeadEnd

Reputation: 23

General SQL/MySQL turning rows into columns

I am currently struggling a bit with MySQL in trying to pivot a table.

A simplified version of the table itself would probably be something along the lines of:

 dayName      amount
---------------------
Monday         34
Tuesday        3453
...            ...

Ideally I would like to be able to turn each day into a column and each amount as its value. Any suggestion on to do it in a clean way?

Thanks!

Upvotes: 1

Views: 36

Answers (1)

CLAbeel
CLAbeel

Reputation: 1088

If your rows are always the days of the week, then you can use something like this:

select 
sum(case when dayName = 'Monday' then amount end) as 'Monday',
sum(case when dayName = 'Tuesday' then amount end) as 'Tuesday' 
.
.
.
from DaysOfWeek;

Unfortunately, MySQL doesn't have a PIVOT function.

http://sqlfiddle.com/#!9/c1a11/6

Upvotes: 1

Related Questions