Also check a method that experiments with pivoting, for up to six values:
;
with Q1 as
(
select id, name,
cast(Addr1 as varchar(max)) as v1,
cast(Addr2 as varchar(max)) as v2,
cast(state as varchar(max)) as v3,
cast(Zip as varchar(max)) as v4
from #Tempaddress
),
Q2 as
(
select id, name, min(t) as t, val
from Q1
unpivot
(
val for t in ( v1, v2, v3, v4 )
) as u
group by id, name, val
),
Q3 as
(
select id, name, val, row_number() over (partition by id, name order by t) as rn
from Q2
)
select id, name, [1], [2], [3], [4], [5], [6]
from Q3
pivot
(
max(val) for rn in ( [1], [2], [3], [4], [5], [6] )
) as p
order by id
Dou you want to make a comma-separated text?