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 2005 Forums
 Transact-SQL (2005)
 Merge Row Data?

Author  Topic 

iqmcl
Starting Member

8 Posts

Posted - 2009-12-05 : 03:48:01
Hi,
I may be missing a very simple solution, but would appreciate some help. I have a simple table, in this example contains 6 rows, I want to merge the second column data, based on email address. So raw data looks like this;

email varcharcolumn
john@smith.com test 1
john@smith.com test 2
john@smith.com test 3
a@b.com test 3
c@d.com test 1
c@d.com test 2

and i want a select statement to return the varchar column concatenated;

john@smith.com test1,test2,test3
a@b.com test3
c@d.com test1,test2

Any help is appreciated.

Cheers

Chris

ddramireddy
Yak Posting Veteran

81 Posts

Posted - 2009-12-05 : 12:43:45
select distinct email, stuff((select ',' + varcharcolumn from tablename where email = t.email for xml path('')),1,1,'') from tablename t
Go to Top of Page
   

- Advertisement -