Greg Larsen (4/27/2015)
Comments posted to this topic are about the item <A HREF="/articles/Stairway+Series/125504/">Stairway to Advanced T-SQL Level 6: Creating Rows Of Data Using The UNPIVOT Operator</A>
Consider using CROSS APPLY instead of UNPIVOT. More flexible (can unpivot multiple columns at once) and can generate better plans.
Unpivot a table using cross apply[/url]
CROSS APPLY VALUES is a more specific term for the use of CROSS APPLY in a manual unpivot. Dwain's written a great article here[/url].
Well it makes no difference if you use VALUES or SELECT with a UNION ALL. That is:
with cte as
select 1 as one, 2 as two, 3 as three, 4 as four
select a, b
select one, two
select three, four
) upvt(a, b)
works just the same if you comment the SELECTs in the cross apply and uncomment the VALUES. Granted, the second is a little less typing but the functionality is the same I believe (as is the actual execution plan, I think). I would even argue that the SELECT...UNION ALL is a little more flexible since you can add where clauses if needed
Gerald Britton, MCSE-DP, MVPToronto PASS Chapter[/url]