create table #temp(ID int, ID2 Varchar(10),Results varchar(50))
insert into #temp values(1,'2',null)
insert into #temp values(1,'3',null)
insert into #temp values(1,'4',null)
insert into #temp values(2,'2',null)
insert into #temp values(2,'3',null)
insert into #temp values(2,'4',null)
insert into #temp values(2,'4',null)
insert into #temp values(3,'1',null)
insert into #temp values(4,'5',null)
insert into #temp values(5,'6',null)
insert into #temp values(5,'6',null)
select * from #temp
Execute below code:
;with cte
as
(
select distinct t.ID,t.ID2,Results,
STUFF((SELECT distinct ', ' + t1.ID2
from #temp t1
where t.[id] = t1.[id]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,2,'') department
from #temp t
)
Update t set t.Results=c.department from
#temp t
inner join cte c
on t.ID=c.ID
No comments:
Post a Comment