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
 General SQL Server Forums
 New to SQL Server Programming
 Combine rows in single row

Author  Topic 

ArpitNarula
Starting Member

16 Posts

Posted - 2011-03-01 : 03:28:07
Hi Guys,
Below is the scenerio:

Table A
Name BU
A 1
A 2
A 3
B 2
C 1
C 3

Output
Name BU
A 1,2,3
B 2
C 1,3

Thanks in Advance

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-03-01 : 04:02:53
check it once....

create table #TableA
(
Name varchar(10),
BU int
)
go

insert into #TableA
select 'A',1 union all
select 'A', 2 union all
select 'A', 3 union all
select 'B', 2 union all
select 'C', 1 union all
select 'C', 3
go

select
Name
,stuff((select ',' + cast(BU as varchar(25)) from #TableA a where a.Name = b.Name for xml path('')), 1, 1, '') as BU
from #TableA b
group by Name

--Ranjit
Go to Top of Page

ArpitNarula
Starting Member

16 Posts

Posted - 2011-03-01 : 05:07:34
Thanks Ranjit...It Worked!

But if you can also guide me/or provide a link which explains how this syntax works that would be great.
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-03-01 : 05:27:00
you are Welcome Arpit,

check the below link
http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=CreateACommaDelimitedList&referringTitle=Home


--Ranjit
Go to Top of Page
   

- Advertisement -