sql server - SQL Query to build a delimited string for a column for each distinct id in a table -
this question has answer here:
i have 4 tables have 2 columns each.
'column 1' 'one' 'column 2' 'many'
column 1 has several different id's need group somehow , build delimited string of 'columns 2' values.
i need every distinct 'column 1' value.... possible?
so example have table..
declare @tbldeadsdata table ( [id] int identity(1,1) not null, [containerid] int null, [deadsid] int null )
it populated data, need build delimited string of [deadsid] each [containerid], , these delimited string need placed table (the deadsdatatable data goes tbllastmerge.deadsidlist in case)..
create table tbllastmerge( [id] int identity(1,1) not null, [feedlotid] int null, [containerid] int null, [containername] varchar(40) null, [ismergetargetcontainer] bit null, [purchaseidlist] varchar(1000) null, [deadsidlist] varchar(1000) null, [railersidlist] varchar(1000) null, [feedbillidlist] varchar(1000) null )
***************************edit********************************* in regards duplicate post.........concatenation not delimited!!!!!! no wonder when did search problem did not see post..... think should link these 2 post though found answer more simplified compared (supposedly) original post
thanks...... found answer
--table hold old deads data declare @tbldeadsdata table ( [id] int identity(1,1) not null, [containerid] varchar(10) null, [deadsid] varchar(10) null )
insert @tbldeadsdata values('1','2'),('1','3'),('1','4'),('2','2'),('2','3'),('2','4') select containerid, deadsid = stuff((select n', ' + deadsid @tbldeadsdata p2 p2.containerid = p.containerid order deadsid xml path(n'')), 1, 2, n'') @tbldeadsdata p group containerid order containerid