TSQL Multiple Rows Into 1 Row

Hello, need help to return multiple rows into 1 singular column with semicolon delimiter.  I tried the FOR XML PATH but got stuck because my original query requires a nested join.  Below is an example of the current results and my expected results.  Thanks in advance!

select a.WorkflowID, b.ResourceName
from table1 a
left join (select distinct ResourceID, ResourceName
			from table2
			where ResourceType = 4) b on a.ConstructID = b.ResourceID
where a.ConstructType = 4

Open in new window

KANEDA 0149Asked:
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.

Éric MoreauSenior .Net ConsultantCommented:
ste5anSenior DeveloperCommented:
Common table expressions are your friend. E.g.

WITH Resources
     FROM   table2
     WHERE  ResourceType = 4 ) ,
AS ( SELECT a.WorkflowID ,
     FROM   table1 a
            LEFT JOIN Resources b ON a.ConstructID = b.ResourceID
     WHERE  a.ConstructType = 4 )
SELECT   O.WorkflowID ,
         STUFF((   SELECT ', ' + I.ResourceName
                   FROM   [Data] I
                   WHERE  I.WorkflowID = O.WorkflowID
                   FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(255)') ,
               1 ,
               2 ,
               '') AS ResourcesText
FROM     [Data] O
GROUP BY O.WorkflowID;

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
KANEDA 0149Author Commented:
Thank you ste5an, that worked!
KANEDA 0149Author Commented:
Thank you, that did it!
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
Query Syntax

From novice to tech pro — start learning today.