Home Forums SQL Server 2008 T-SQL (SS2K8) Unpivot question, struggling with data please help RE: Unpivot question, struggling with data please help

  • Let me explain a little further about the CROSS APPLY.

    CROSS APPLY (

    VALUES (Column1a, Column2a, Column3a, Column4a)

    ,(Column1b, Column2b, Column3b, Column4b)

    ,(Column1c, Column2c, Column3c, Column4c)

    ,(Column1d, Column2d, Column3d, Column4d)) alphas(col1, col2, col3, col4)

    Looking down through the columns constructed by VALUES, for col1 it consists of Column1a, Column1b, Column1c and Column1d. So all of these must be of the same data type (or CAST to the same data type).

    To add more columns, e.g., e, f and g, you'd want something like:

    CROSS APPLY (

    VALUES (Column1a, Column2a, Column3a, Column4a)

    ,(Column1b, Column2b, Column3b, Column4b)

    ,(Column1c, Column2c, Column3c, Column4c)

    ,(Column1d, Column2d, Column3d, Column4d)

    ,(Column1e, Column2e, Column3e, Column4e)

    ,(Column1f, Column2f, Column3f, Column4f)

    ,(Column1g, Column2g, Column3g, Column4g)

    ) alphas(col1, col2, col3, col4)

    The number of columns produced in table "alphas" corresponds to the number of columns appearing in each () grouping of the VALUES set (Table Row Constructor).


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St