question

dwarburton avatar image
dwarburton asked

Combine field from two rows into single field

Current row; Name Bldg CID John Doe 120 450 Next row; Name Bldg CID John Doe 120 500 Want to remove duplicate rows but add to the CID field of the other rows values Results; Name Bldg CID John Doe 120 450;500
t-sqlmssqlduplicate values
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
David Wimbush avatar image
David Wimbush answered
Something like this should do the trick: select a.[Name] , a.Bldg , stuff (( select top 100 percent ';' + CID from b where b.[Name] = a.[Name] and b.Bldg = a.Bldg order by b.CID for xml path('') ), 1, 1, '') as CIDs from ( select distinct [Name] , Bldg from ) a order by a.[Name] , a.Bldg;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.