Need Sub-Query ?

  • Create Table Table1

    (

    Sno int,

    Sname varchar(20)

    );

    insert into table1 values(1,'a'),

    (2,'b'),

    (1,'c'),

    (2,'d')

    select * from Table1

    Sno Sname

    -- ------

    1 a

    2 b

    1 c

    2 d

    i want the output as following by using the sub-queries?

    Sno Name1 Name2

    1 a c

    2 b d

  • This should give you some idea

    SELECTSno,

    MAX( CASE WHEN RN = 1 THEN Sname ELSE NULL END ) AS Name1,

    MAX( CASE WHEN RN = 2 THEN Sname ELSE NULL END ) AS Name2

    FROM(

    SELECTROW_NUMBER() OVER ( PARTITION BY Sno ORDER BY Sname ) AS RN, *

    FROMTable1

    ) AS T

    GROUP BY Sno


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks For The Quick Reply KingSton

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

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