日期:2014-05-17 浏览次数:20686 次
--> 测试数据:@表A
declare @表A table([A01] varchar(4))
insert @表A
select 'a001' union all
select 'a002' union all
select 'a003'
--> 测试数据:@表B
declare @表B table([B01] varchar(4),[B02] int)
insert @表B
select 'a001',11 union all
select 'a001',22 union all
select 'a001',33 union all
select 'a002',44 union all
select 'a003',55 union all
select 'a003',66
select a.[A01],b.B02 AS A02 from @表A a
LEFT JOIN
(
select [B01], [B02]=stuff((select '-'+LTRIM([B02])
from @表B where [B01]=t.[B01] for xml path('')), 1, 1, '')
from @表B t group by [B01]
) b ON a.A01=b.B01
/*
A01 A02
---- -----------
a001 11-22-33
a002 44
a003 55-66
*/
create table 表A(A01 varchar(10))