0

I don't even know how to phrase this question to search it. Here's my table:

enter image description here

I want to put this into a table in MySQL that would look like this:

enter image description here

I have no idea how to automatically accomplish this. The actual table I'm using is too big to do manually like this example. Any guidance or help would be appreciated, thanks!

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
ajthyng
  • 1,245
  • 1
  • 12
  • 18

2 Answers2

0

MySQL does not have a built in function for unpivoting, so you will have to use UNION ALL:

SELECT 'A' AS Country, Date, A As 'Amount'
FROM yourTable
UNION ALL
SELECT 'B' AS Country, Date, B As 'Amount'
FROM yourTable
 UNION ALL
SELECT 'C' AS Country, Date, C As 'Amount'
FROM yourTable
UNION ALL
SELECT 'D' AS Country, Date, D As 'Amount'
FROM yourTable
UNION ALL
SELECT 'E' AS Country, Date, E As 'Amount'
FROM yourTable
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you. I didn't know unpivoting was what I was trying to do! I used microsoft's powerquery addon to do it then just loaded my table as a CSV. – ajthyng Feb 15 '16 at 04:27
0
SELECT 'SELECT ''' + [COLUMN_NAME] +''' as Country, Date, Amount
FROM yourTable
WHERE Country ='''   + [COLUMN_NAME] +''' UNION'
FROM [INFORMATION_SCHEMA].[COLUMNS]
WHERE [TABLE_NAME] ='yourTable'

And than the result would be the SQL you want.

Kason
  • 787
  • 3
  • 14