How to select an single rows to multiple columns

  • I am Ramanathan and working in SQL SERVER 2008….Please help me

    Table1

    Location--Field

    A1

    A2

    A3

    B1

    B2

    B3

    How to change an below format from table1

    AB--Field

    A1B1

    A2B2

    A3B3

  • declare @tab table (col1 char(2))

    insert into @tab values

    ('A1'),

    ('A2'),

    ('A3'),

    ('B1'),

    ('B2'),

    ('B3')

    SELECT

    A.Col1,

    B.Col1

    FROM

    (

    SELECT

    ROW_NUMBER() OVER(PARTITION BY LEFT(Col1,1) ORDER BY Col1) AS RowNum,

    Col1

    FROM

    @tab

    WHERE

    LEFT(Col1,1) = 'A'

    ) AS A

    FULL OUTER JOIN

    (

    SELECT

    ROW_NUMBER() OVER(PARTITION BY LEFT(Col1,1) ORDER BY Col1) AS RowNum,

    Col1

    FROM

    @tab

    WHERE

    LEFT(Col1,1) = 'B'

    ) AS B

    ON

    A.RowNum = B.RowNum

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

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