~高手帮忙,实现一个函数实现相同部分留下,不同部分连接起来!
如下所示:
id name str
1 sf a
1 sf b
2 sv c
2 sv d
...
结果为:
id name str
1 sf ab
2 sv cd
......
先谢过。。。。
------解决方案--------------------create table T([id] varchar(10), [name] varchar(10), [str] varchar(10))
insert T select 1, 'sf ', 'a '
union all select 1, 'sf ', 'b '
union all select 2, 'sv ', 'c '
union all select 2, 'sv ', 'd '
create function fun(@id varchar(10), @name varchar(10))
returns varchar(100)
as
begin
declare @re varchar(100)
set @re= ' '
select @re=@re+[str] from T where [id]=@id and [name]=@name
return @re
end
select [id], [name], [str]=dbo.fun([id], [name]) from T group by [id], [name]
--result
id name str
---------- ---------- --------
1 sf ab
2 sv cd
(2 row(s) affected)