create table #rows (id uniqueidentifier, value varchar(10))
insert into #rows
values ('11111111-1111-1111-1111-111111111111', 'a'),
('22222222-2222-2222-2222-222222222222', 'b'),
('11111111-1111-1111-1111-111111111111', 'c'),
('22222222-2222-2222-2222-222222222222', 'd'),
('11111111-1111-1111-1111-111111111111', 'e'),
('33333333-3333-3333-3333-333333333333', 'f'),
('33333333-3333-3333-3333-333333333333', 'g')
create table #result (id uniqueidentifier, value varchar(10))
create clustered index idx_result on #result(id) -- for ordered
insert into #result
select * from #rows order by id -- Important: result must be ordered by id
declare @value varchar(max),
@id uniqueidentifier = '00000000-0000-0000-0000-000000000000'
update #result
set
@value = value = case
when @id <> id then isnull(value,'')
else @value + isnull(value,'')
end,
@id = id
select id, max(value) from #result group by id
Last active
June 17, 2018 20:12
-
-
Save serjKim/653cb774ebe72f058f5e to your computer and use it in GitHub Desktop.
T-SQL: Fast concat table rows
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment