There is a question here in stackoverflow with the same title but that is not what I am looking for.
I have a table like the one below
Name | Count
----------------
Chery | 257
Drew | 1500
Morgon | 13
Kath | 500
Kirk | 200
Matt | 76
I need to trasform this result set into something like this
Chery | Drew | Morgon | Kath | Kirk | Matt
-------------------------------------------
257 1500 13 500 200 76
How do i acheive this using sql server 2005?
See Using PIVOT and UNPIVOT.
The quick answer is
If you want to avoid anything complicated like a pivot or the accepted answer, you can do this! (most of the code is just setting up the test data just in case anyone wants to try it)
There are similar questions here,here answered in stackoverflow.
You need to use the operator PIVOT in your query to acheive this.Here is the example and explanation on how you can do that.The example is referenced from this source.
Explanation
1.The first part of the query
gives you a nice flattened result of your Name column values in a single row as follow
You can learn more about the STUFF and XML PATH here and here.
2.
SELECT + @cols + FROM
will select all the rows as coloumn names for the final result set (pvt - step 3)i.e
3.This query pulls all the rows of data that we need to create the cross-tab results. The (p) after the query is creating a temporary table of the results that can then be used to satisfy the query for step 1.
4.The PIVOT expression
does the actual summarization and puts the results into a temporary table called pvt as