Need help with this query

  • Hi all,

    I have to make the following, but I've had no luck getting it done. Can you please help? Thanks in advance.

    Before:

    Col1 Col2 Col3 Col4

    1 2 A 1

    1 2 B 2

    1 2 C 3

    After:

    Col1 Col2 Col3_1 Col3_2 Col3_3

    1 2 A B C

    The data includes only 3 rows max for combination of Col1 and Col2. And the results have to be ordered using Col4, Ascending. Thanks again.

    There can only be

  • What have you tried?

    a couple hints.

    Use the case statement, wrapped in Min, or Max

    Group by Col 1, Col 2

    Check this out.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • That's exactly what I am trying right now! 🙂

    I got this so far:

    SELECT col1,

    col2,

    CASE col4

    WHEN 1 THEN col3

    ELSE NULL

    END AS col3_1,

    CASE col4

    WHEN 2 THEN col3

    ELSE NULL

    END AS col3_2,

    CASE col4

    WHEN 3 THEN col3

    ELSE NULL

    END AS col3_3

    FROM table1

    GROUP BY col1,

    col2

    However, when I run this I get the following errors:

    Msg 8120, Level 16, State 1, Line 1

    Column col3 is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    Msg 8120, Level 16, State 1, Line 1

    Column col4 is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    I'm working on it, but if you have the solution, please don't hesitate to post it. Thanks for your input by the way.

  • Forgot, I have also tried PIVOT.

  • I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm. Thanks!

  • shahgols (11/12/2012)


    I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm. Thanks!

    Outstanding! Would you post your solution so that others may learn from it? Thanks!

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • GOOD idea, sure, here is the code:

    SELECT col1,

    col2,

    Max(CASE col4

    WHEN 1 THEN col3

    ELSE NULL

    END) AS col3_1,

    Max(CASE col4

    WHEN 2 THEN col3

    ELSE NULL

    END) AS col3_2,

    Max(CASE col4

    WHEN 3 THEN col3

    ELSE NULL

    END) AS col3_3

    FROM table1

    GROUP BY col1,

    col2

    And in case you are wondering, formatting of the above SQL was done at this page (with whom I have no affiliation with):

    http://www.dpriver.com/pp/sqlformat.htm

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

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