1

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?

1
  • There are countless examples of Pivot both dynamic and static. However, the inital concern is how to enforce the proper sequence. Commented Jan 24, 2018 at 0:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't even know about Cross Apply, this is much simpler than using Pivot, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.