user3277335
user3277335

Reputation: 2037

SQL Pivot Table Multiple Columns

I have a table that looks like this:

Title01    Title02     Title03       Title04        Title05
Number     Title       Division      Department     IFC

And I am wanting to turn the columns into rows so it loos like this:

Field
Number
Title
Division
Department
IFC

Is it possible to do this using the PIVOT function in SQL?

Upvotes: 1

Views: 80

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

I like to use CROSS APPLY for this:

select v.field
from t cross apply
     (values (title01), (title02), (title03), (title04), (title05)
     ) v(field);

CROSS APPLY implements the lateral join. You can think of it as an extension of correlated subqueries -- but the subquery can return multiple columns and multiple rows. Unpivoting data happens to be a simple introduction to the concept.

Upvotes: 1

Related Questions