BenDundee
BenDundee

Reputation: 4521

Transpose SQL result set

I have data in the following format:

Machine |  week_end  | widgets
 A      | 05-26-2013 | 5
 B      | 05-26-2013 | 4
 A      | 04-01-2013 | 6
 B      | 04-01-2013 | 0

I want my result set to look as follows:

Machine | 05-26-2013 | 04-01+2013 | ...
 A      | 5          | 6          |
 B      | 4          | 0          |

I'm not quite sure how to proceed here--how does one transpose a table like this without having to specify the week ends (or whatever) explicitly in the query? (DB is SQL Server 2008.)

Upvotes: 1

Views: 590

Answers (1)

Nicholas Post
Nicholas Post

Reputation: 1857

You will need to do some research on the SQL PIVOT function. It will allow you to turn rows into columns. This should be what you are looking for, once you find out how to apply it.

Check out this link: Using PIVOT and UNPIVOT

Upvotes: 2

Related Questions