How to invert rows and columns using a T-SQL Pivot Table

Posted by Jeff Stock on Stack Overflow See other posts from Stack Overflow or by Jeff Stock
Published on 2010-03-23T19:57:16Z Indexed on 2010/03/23 20:03 UTC
Read the original article Hit count: 266

Filed under:
|

I have a query that returns one row. However, I want to invert the rows and columns, meaning show the rows as columns and columns as rows. I think the best way to do this is to use a pivot table, which I am no expert in.

Here is my simple query:

SELECT Period1, Period2, Period3
FROM GL.Actuals
WHERE Year = 2009 AND Account = '001-4000-50031'

Results (with headers):

Period1, Period2, Period3

612.58, 681.36, 676.42

I would like for the results to look like this:

Desired Results:

Period, Amount

Period1, 612.58

Period2, 681.36

Period3, 676.42

This is a simple example, but what I'm really after is a bit more comlex than this. I realize I could produce theses results by using several SELECT commands instead. I'm just hoping someone can shine some light on how to accomplish this with a Pivot Table or if there is yet a better way.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql