不用函数 如果每个只有2个值 可以这样 create table #t ( aa varchar(30), bb varchar(30))insert into #t(aa,bb) select 'A1','A' union all select 'A2','A' union all select 'B1','B' union all select 'B2','B' union all select 'C1','C' select cast(min(aa) as varchar(10))+cast(max(aa) as varchar(10)) as aa,bb from #t group by bb drop table #t
create table t_unit(AA varchar(20) , BB varchar(20))insert into t_unit select 'A1', 'A' union select 'A2', 'A' union select 'B1', 'B' union select 'B2', 'B' union select 'C2', 'C'create function f_unit( @BB varchar(5000)) returns varchar(5000) as begin declare @ret varchar(5000) set @ret = '' select @ret = @ret + AA from t_unit where BB = @BB return @ret endselect dbo.f_unit(BB) , BB from t_unit group by BBdrop table t_unit drop function f_unit
如果每个只有2个值
可以这样
create table #t
(
aa varchar(30),
bb varchar(30))insert into #t(aa,bb)
select 'A1','A'
union all
select 'A2','A'
union all
select 'B1','B'
union all
select 'B2','B'
union all
select 'C1','C'
select cast(min(aa) as varchar(10))+cast(max(aa) as varchar(10)) as aa,bb
from #t
group by bb
drop table #t
create table t_unit(AA varchar(20) , BB varchar(20))insert into t_unit
select 'A1', 'A' union
select 'A2', 'A' union
select 'B1', 'B' union
select 'B2', 'B' union
select 'C2', 'C'create function f_unit( @BB varchar(5000))
returns varchar(5000)
as
begin
declare @ret varchar(5000)
set @ret = ''
select @ret = @ret + AA
from t_unit
where BB = @BB
return @ret
endselect dbo.f_unit(BB) , BB
from t_unit
group by BBdrop table t_unit
drop function f_unit