Concatenate Several Records into One Field

I found this function online and now need help making it work.  Here is my data:

Loan Number   Category
12345                 Approval missing
12345                 Income_Misc
12345                 Insurance_Hazard Insurance Premium: Require proof of paid
55555                 HUD1_Misc
55555                 Property_Misc
55555                 Credit_Paid-off Accounts: Require evidence closed

Here's the query I currently have:

SELECT LoanNumber,
substring((SELECT ( ', ' + Category) FROM SummaryReportView t ORDER BY Category ASC FOR XML PATH( '' )), 3, 1000 ) AS Category
FROM LoanData

Results:
Loan Number   Category
12345                Approval missing, Credit_Paid-off Accounts: Require evidence closed, HUD1_Misc, Income_Misc, Insurance_Hazard            
                           Insurance Premium: Require proof of paid, Property_Misc
55555                Approval missing, Credit_Paid-off Accounts: Require evidence closed, HUD1_Misc, Income_Misc, Insurance_Hazard            
                           Insurance Premium: Require proof of paid, Property_Misc

As you can see, the Category field is pulling Categories from all Loans.  I need each loan to only have the categories on that loan's record.

Please help.
Thanks,
Scott
Scott WilliamsData AnalystAsked:
Who is Participating?

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

x
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You'll need to join the CategoryID (I'm assuming there's a category ID in both tables):
SELECT LoanNumber, 
 substring((SELECT ( ', ' + Category) FROM SummaryReportView t 
             WHERE LoanData.CategoryID = t.CategoryID
              ORDER BY Category ASC FOR XML PATH( '' )), 3, 1000 ) AS Category
 FROM LoanData

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
Brian CroweDatabase AdministratorCommented:
DECLARE @MyTable TABLE
(
	LoanNumber		INT,
	Category		VARCHAR(100)
);

INSERT INTO @MyTable (LoanNumber, Category)
VALUES (12345, 'Approval missing'),
	(12345, 'Income_Misc'),
	(12345, 'Insurance_Hazard Insurance Premium: Require proof of paid'),
	(55555, 'HUD1_Misc'),
	(55555, 'Property_Misc'),
	(55555, 'Credit_Paid-off Accounts: Require evidence closed')

SELECT T.LoanNumber,
	STUFF(
	(
		SELECT ', ' + Category
		FROM @MyTable
		WHERE LoanNumber = T.LoanNumber
		ORDER BY Category
		FOR XML PATH('')
	), 1, 2, '') AS CategoryList
FROM @MyTable AS T
GROUP BY LoanNumber

Open in new window

Scott WilliamsData AnalystAuthor Commented:
Perfect Vitor.  Thanks for this consolidated solution!
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.