SELECT query help

  • I have the following data in a table

    col1 col2 col3 col4 col5

    1 122 AAA null null

    2 122 null BBB null

    3 122 null null CCC

    4 156 null BBB null

    5 156 AAA null null

    6 156 null null CCC

    I am trying to get the follwing result

    122 AAA BBB CCC

    156 AAA BBB CCC

    ( The not null value for each unique col2)

    Thank you.

  • 1) what have you tried?

    2) do you just have the five columns?

  • something like this??

    DECLARE @Input TABLE

    (

    col1 INT,

    col2 INT,

    col3 VARCHAR(3),

    col4 VARCHAR(3),

    col5 VARCHAR(3)

    )

    INSERT INTO @Input VALUES(1, 122, 'AAA', null, null),(2, 122, null, 'BBB', null),(3, 122, null, null, 'CCC'),

    (4, 156, null, 'BBB', null), (5, 156, 'AAA', null, null), (6, 156, null, null, 'CCC')

    SELECT col2, MAX(col3) as col3, MAX(col4) as col4, MAX(col5) as col5

    FROM @Input

    GROUP BY col2

    Good Luck 🙂 .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.

Viewing 3 posts - 1 through 2 (of 2 total)

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