SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stairway to Advanced T-SQL Level 6: Creating Rows Of Data Using The UNPIVOT Operator


Stairway to Advanced T-SQL Level 6: Creating Rows Of Data Using The UNPIVOT Operator

Author
Message
Greg Larsen
Greg Larsen
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13031 Visits: 290
Comments posted to this topic are about the item Stairway to Advanced T-SQL Level 6: Creating Rows Of Data Using The UNPIVOT Operator

Gregory A. Larsen, MVP
Gerald Britton
Gerald Britton
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9479 Visits: 1793
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

Gerald Britton, MCSE-DP, MVPToronto PASS Chapter
Iwas Bornready
Iwas Bornready
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49858 Visits: 886
I'm still a little confused, but good start for me anyway.
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)

Group: General Forum Members
Points: 129432 Visits: 21230
g.britton (6/24/2015)
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


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.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Gerald Britton
Gerald Britton
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9479 Visits: 1793
ChrisM@Work (6/25/2015)
g.britton (6/24/2015)
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


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.


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
from cte
cross apply
(
select one, two
union all
select three, four
--values (one,two),
-- (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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search