vendredi 19 décembre 2014

Turning multiple fields in column to row


I have an issue that has really stumped me in MSSQL


I have recently had the need to insert a row in to a table, made up from a number of fields. I want to make the one row up from something like the below.



id | Key | Value
1 Firstname Tim
2 Secondname Skold
3 Age 48
4 Gender Male


So the output should be something like:



id | Firstname | Secondname | Age | Gender
1 Tim Skold 48 Male


I have tried to pivot the result, but can't get it to work, and have also tried to union the results, but this gives the results as a column, not as a single row.


The messy way in which this is being done currently is by joining on to the same table multiple times to get each field (firstname, secondname etc...)


Maybe unpivoting?


Really not sure, thanks for any help...


James.





Aucun commentaire:

Enregistrer un commentaire