Robert Koritnik
Robert Koritnik

Reputation: 105019

Transpose a database table to dynamic column count result

There are several transposing questions on Stackoverflow, but looking at few non of them is really similar to my problem. The main difference being: the have a predefined set of columns.

Let's say my table looks like this:

ID Name   Value
---------------
1  Set    Mitch
2  Get    Jane
3  Push   Dave
4  Pull   Mike
5  Dummy  John
...

I'd like to transpose it to become:

Set    Get   Push  Pull  Dummy ...
----------------------------------
Mitch  Jane  Dave  Mike  John  ...

Upvotes: 0

Views: 3935

Answers (2)

jon_darkstar
jon_darkstar

Reputation: 16768

Do you need to do this in the SQL? It seems pretty trivial if you can just do it after you get a SELECT * query into an array you can manipulate at will.

Upvotes: -1

Pondlife
Pondlife

Reputation: 16240

It looks like you're looking for a "dynamic pivot table". See the example here, or Google that term for more information:

http://www.kodyaz.com/articles/t-sql-pivot-tables-in-sql-server-tutorial-with-examples.aspx

Upvotes: 2

Related Questions