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

  • Greg Larsen

    SSC-Insane

    Points: 20605

    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

  • g.britton

    SSChampion

    Points: 13685

    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, MCSE-DP, MVPToronto PASS Chapter[/url]

  • akljfhnlaflkj

    SSC Guru

    Points: 76202

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

  • ChrisM@Work

    SSC Guru

    Points: 186043

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

    [font="Arial"]“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw[/font]


    For fast, accurate and documented assistance in answering your questions, please read this article[/url].
    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]
    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
    [url

  • g.britton

    SSChampion

    Points: 13685

    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, MCSE-DP, MVPToronto PASS Chapter[/url]

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

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