need one query to get the result as shown

  • Hi

    I have two tables with following data

    Table1

    Col1

    ----

    11

    22

    33

    44

    Table2

    Col1 Col2

    ---- -----

    11 ab

    11 ac

    11 ad

    22 jk

    22 jy

    44 tttt

    i need to get the output like

    Table

    Col1 Col2

    ---- -----

    11 ab,ac,ad

    22 jk,jy

    33 (empty) -- because no values

    44 tttt

    Waiting for Reply

    Thanks in Advance

  • Oops, I missed that you wanted all the values of B.Col2 on one line. This does not get you that. Removed code. Sorry

    -- Cory

  • declare @t_table1 table

    (Col1int)

    insert into @t_table1

    select 11 union

    select 22 union

    select 33 union

    select 44

    declare @t_table2 table

    (Col1int,

    Col2varchar(10))

    insert into @t_table2

    select 11, 'ab' union

    select 11, 'ac' union

    select 11, 'ad' union

    select 22, 'jk' union

    select 22, 'jy' union

    select 44, 'tttt'

    select t1.Col1,

    stuff((SELECT ', ' + replace(Col2, ' ', '')

    FROM @t_table2 t2

    WHERE t2.Col1 = t1.Col1

    ORDER BY Col2

    FOR XML PATH('')), 1, 2, '') AS Vals

    from @t_table1 t1

    order by t1.Col1

    └> bt



    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Thanks a lot sir

    It is working for me...

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

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