The result of a query gives the following format (pipes '|' represent column delimiters) :
Query: SELECT DISTINCT Description, Cat FROM SampleTable
Description1 | Cat1
Description1 | Cat2
Description1 | Cat3
Description1 | Cat4
Description2 | Cat1
Description2 | Cat2
Description3 | Cat3
Description3 | Cat4
I would like to format these lines like this:
Description1 | Cat1,Cat2,Cat3,Cat4
Description2 | Cat1,Cat2
Description3 | Cat3,Cat4
Is there a way to do this with a T-SQL command ?
hi hope this helps
create sample data script
create table #SampleTable ( Description varchar(20), Cat varchar(10))
insert into #SampleTable select 'Description1','Cat1'
insert into #SampleTable select 'Description1','Cat2'
insert into #SampleTable select 'Description1','Cat3'
insert into #SampleTable select 'Description1','Cat4'
insert into #SampleTable select 'Description2','Cat1'
insert into #SampleTable select 'Description2','Cat2'
insert into #SampleTable select 'Description3','Cat3'
insert into #SampleTable select 'Description3','Cat4'
select * from #SampleTable
select
description
, STRING_AGG (Cat, ',') as Cat_Agg
from
#SampleTable
group by
description
1 Like