I'm trying to learn how to import data from a csv file to my database and then reorganise it into my tables. I have imported a very simple set of data into a table called ' CSVTest_Match' that looks like this:
HEADER DATA --(Column Names)
--------------- --------------
Home Team Barnsley
Away Team Wigan
Kick Off Time 14:02
Kick Off Date 03/08/2013
Home Goals 0
Away Goals 4
The values in both columns are VARCHAR's at this point. I would like to transform this data to look like this:
HOMETEAM AWAYTEAM KICKOFFTIME KICKOFFDATE HOMEGOALS AWAYGOALS -- (Column Names)
---------- ---------- ------------- ------------- ----------- ----------
Barnsley Wigan 14:02 03/08/2013 0 4
At this point it would be useful if the data was converted to VARCHAR, DATETIME, TINYINT values as appropriate. I've been getting rather confused trying to work out how to use PIVOT to do this so I would really appreciate some help.
EDIT: I finally figured it out. The code required was:
SELECT * FROM
(SELECT Header, Data FROM CSVTest_Match) AS T
PIVOT (Min(Data) FOR Header IN ([Home Team], [Away Team], [Kick Off Time],
[Kick Off Date], [Home Goals], [Away Goals])) AS T2