SQL Server : Concatenate the values of the same column to get one column values

Hi I want to build a query that allows me to concatenate the differents values of the same column for the same line ID .... it is a kind of Group By ......

Attached you will see the view that I built and the view lines ..... Also I attached a code I found within some searches on the web but I didn't figure out how to adapt it to my case.

The final result should be a view that will get for each LineID ------> Value1, Value2, Valuex and so on .....  
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If I understand you correctly, this is what you need.

In  your case, you can separate the values by comma or whatever your requirements are.


This is the example code I used from that link, the part that says use when values are unknown.

WITH CTE ( Id, employee_id, charity_code, length ) 

          AS ( SELECT Id, CAST( '' AS VARCHAR(8000) ), CAST( '' AS VARCHAR(8000) ), 0

                 FROM employee_ded_amts

                GROUP BY Id

                UNION ALL

               SELECT p.Id, CAST( p.employee_id + '++'+

                      CASE WHEN length = 0 THEN '' ELSE ', ' END + p.charity_code AS VARCHAR(8000) ), 

                      CAST( p.charity_code AS VARCHAR(8000)), length + 1

                 FROM CTE c

                INNER JOIN employee_ded_amts p

                   ON c.Id = p.Id

                WHERE p.charity_code > c.charity_code )

SELECT Id, employee_id 

      FROM ( SELECT Id, employee_id, 

                    RANK() OVER ( PARTITION BY Id ORDER BY length DESC )

               FROM CTE ) D ( Id, employee_id, rank )

     WHERE rank = 1 ;

Open in new window

I used ++ as delimiter and I got values like this screenshot.

In my example, I concatenated only 2 column names and I used ID as identifier.

like this...

select quoteid,quotenumber,projectname,numberofunits,representative,contactname
    ,Stuff((select ', '+taskname
              from quotetask as a
             inner join kpitasks as b
               on a.kpitaskid=b.kpitaskid
             where a.quoteid=w.quoteid
             order by taskname
             for xml path('')),1,2,'') as Tasks

from quoteworkflow as w
order by 1

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
venmarcesAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.