• sharonsql2013 (9/9/2015)


    I am trying to get the 3 rows in the below in one line. with all 3 names separated by ","

    create table #Product

    (

    Application Varchar(50),

    Name varchar(10),

    Counts Int

    )

    insert into #Product values('Excel', 'Dave', 1)

    insert into #Product values('Excel', 'Ken', 10)

    insert into #Product values('Excel', '', 4)

    insert into #Product values('SQL', 'Tim', 6)

    Select * from #Product

    However I want to get all 3 rows in 1 but can't get all columns . I can do so only with name column. Please advise

    SELECT distinct STUFF((SELECT ',' + Name

    FROM #Product A

    ORDER BY [Application]

    FOR XML PATH('')), 1, 1, '') AS [Output]

    You provided much of what was needed in your OP (Original Post), but you didn't really show what you expected as a result. Something like this would have helped a lot:

    create #ExpectedResults (

    Application varchar(50),

    UserNames varchar(2000) -- not sure how many 10 character names you may concatenate

    );

    insert into #ExpectedResults

    values ('Excel','Dave, Ken'),('SQL','Tim');

    select * from #ExpectedResults;