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

  • 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

  • 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]

    Gerald Britton, Pluralsight courses

  • I'm still a little confused, but good start for me anyway.

  • 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[/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].

    “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

  • 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[/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

    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, Pluralsight courses

  • This was removed by the editor as SPAM

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply