How to concatenate multiple rows into a single column base on primary key using SQL Server (transact SQL)

I have the following table:

First.jpg
My Desired output:

Second.jpg
I am concatenating many rows into a single column text string; so how this can be done with detailed explanation.
HarreniAsked:
Who is Participating?
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:
1
HarreniAuthor Commented:
Thanks a lot Éric for your prompt help and it's work.
After I did this concatenation, What if I want to segregate "each value" in "FileNameString column" in a separate new column? it's limited to six column only "AAA, BBB, CCC, DDD, EEE, FFF"?
0
Vikas GargBusiness Intelligence DeveloperCommented:
Hi

SELECT DISTINCT p.FieldId,
  STUFF((SELECT distinct ',' + p1.FieldName
         FROM Table p1
         WHERE p.FieldId = p1.FieldId
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'') FieldNameString
FROM Table p;

Open in new window

0

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
Éric MoreauSenior .Net ConsultantCommented:
This is a different story then!

Somebody will probably come up with a pivot. I would probably go that way:
CREATE TABLE #data (
	FileID	INT,
	FileName VARCHAR(50)
)

INSERT INTO #data ( FileID, FileName )
VALUES  ( 1, 'AAA' )
, (2, 'BBB')
, (3, 'CCC')
, (1, 'DDD')
, (1, 'EEE')
, (2, 'FFF')

; WITH cte(FileID, FileName, RowId)
AS (
	SELECT *, ROW_NUMBER() OVER(PARTITION BY FileID ORDER BY FileID, FileName) 
	FROM #data
)

SELECT C1.FileID, C1.FileName, C2.FileName, C3.FileName, C4.FileName
FROM cte AS C1
LEFT JOIN cte AS C2 ON C2.FileID = C1.FileID AND C2.RowId = 2
LEFT JOIN cte AS C3 ON C3.FileID = C1.FileID AND C3.RowId = 3
LEFT JOIN cte AS C4 ON C4.FileID = C1.FileID AND C4.RowId = 4
WHERE C1.RowId = 1

DROP TABLE #data

Open in new window

0
HarreniAuthor Commented:
Thanks Experts for your great help
0
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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.