Unfortunately, SQL Server does not have a concatenation function, but you can use the xml trick:
select
t.ID, t.Value1, t.Value2, t.Value3,
case
when count(*) over(partition by t.Value1, t.Value2) > 1 then 1
else 0
end as Duplicate,
stuff(
(
select ' - ' + TT.Value3
from @Table as TT
where TT.Value1 = t.Value1 and TT.Value2 = t.Value2
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 3, '') as Value3_Dupl
from @Table as t
sql
If you need to show duplicates just for Duplicate = 1, I think it's better to use cte (or a subquery), a little cleaner:
;with cte as (
select *, count(*) over(partition by Value1, Value2) as cnt
from @Table
)
select
t.ID, t.Value1, t.Value2, t.Value3,
case
when cnt > 1 then 1
else 0
end as Duplicate,
case
when cnt > 1 then
stuff(
(
select ' - ' + TT.Value3
from @Table as TT
where TT.Value1 = t.Value1 and TT.Value2 = t.Value2
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 3, '')
end as Value3_Dupl
from cte as t
sql
. type for xml path(''), value('.', 'nvarchar(max)'), , . solutin varchar, '<' '&' .