Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 [RESOLVED] merge 2 or more records

Author  Topic 

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2013-10-14 : 18:21:21
I have a table with multiple records.
No | Type |
ZA123 | TCP |
ZA123 | XCP |
GT456 | POI |
GT456 | HYR |

I would need a result set that combines the Type values, like:
ZA123 | TCP; XCP

How would I approach this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-15 : 00:49:59
[code]
SELECT t.No,
STUFF((SELECT ';' + [Type] FROM Table WHERE [No] = t.[No] FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT [No] FROM Table)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-15 : 00:50:00
[code]
SELECT t.No,
STUFF((SELECT ';' + [Type] FROM Table WHERE [No] = t.[No] FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT [No] FROM Table)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2013-10-15 : 01:00:26
SELECT [No] +' | ' + STUFF((SELECT ' ; ' + [Type] FROM #T WHERE [No] = t.[No] FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT [No] FROM #T)t

veeranjaneyulu
Go to Top of Page

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2013-10-15 : 10:16:12
perfect - thanks
Go to Top of Page
   

- Advertisement -