SQLServer中字符串拼接并去掉重复?该怎么解决

SQLServer中字符串拼接并去掉重复?SQLServer中可实现字符串的简单相加拼接.如果数据情况为此,则如何实现?

SQLServer中字符串拼接并去掉重复?
SQLServer中可实现字符串的简单相加拼接.
如果数据情况为此,则如何实现?
表T:F1,F2 数据如下:中间用空格搁开.
F1 F2
A x,y,z
A y,z,p
B x,y
B x,z
C e,x
C e,d

分组实现结果为:
A,x,y,z,p
B,x,y,z
C,e,x,d


[解决办法]
先拆分表,然后去重复,然后合并表.

拆分表:

CREATE TABLE A(id INT,country VARCHAR(100))
INSERT A
SELECT 1,'中国;日本;韩国' UNION ALL
SELECT 2,'美国;意大利;法国' UNION ALL
SELECT 3,'德国'
SELECT * FROM A

-- 建立一个辅助的临时表就可以了
SELECT TOP 8000 id = identity(int,1,1) 
INTO # FROM syscolumns a, syscolumns b

 
SELECT 
A.ID, 
COUNTRY = SUBSTRING(A.COUNTRY, B.ID, CHARINDEX(';', A.COUNTRY + ';', B.ID) - B.ID) 
FROM A, # B
WHERE SUBSTRING(';' + a.COUNTRY, B.id, 1) = ';'
ORDER BY 1,2
GO

DROP TABLE A,#

id country
----------- ----------------
1 中国;日本;韩国
2 美国;意大利;法国
3 德国

(所影响的行数为 3 行)

ID COUNTRY
----------- ---------
1 韩国
1 日本
1 中国
2 法国
2 美国
2 意大利
3 德国

(所影响的行数为 7 行)

去重复:这里不说了.

合并表

--带符号合并行列转换

--有表t,其数据如下:
a b
1 1
1 2
1 3
2 1
2 2
3 1
--如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1 

create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
go

if object_id('pubs..f_hb') is not null
drop function f_hb
go

--创建一个合并的函数
create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(b as varchar) from tb where a = @a 
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct a ,dbo.f_hb(a) as b from tb

drop table tb

--结果
a b
----------- ------
1 1,2,3
2 1,2
3 1

(所影响的行数为 3 行)
[解决办法]
CREATE TABLE A(f1 char(10),f2 VARCHAR(100)) 
INSERT A 
select 'A', 'x,y,z' union all
select 'A', 'y,z,p' union all 
select 'B', 'x,y' union all 
select 'B', 'x,z' union all 
select 'C', 'e,x' union all 
select 'C', 'e,d' 
select * from a

select top 1000 id=identity(int,1,1) into # from syscolumns a,syscolumns b

select DISTINCT case A.f1 when 'A' then 1 when 'B' THEN 2 ELSE 3 END AS F1 ,F2=SUBSTRING(A.F2,B.ID,CHARINDEX(',',A.F2+',',B.ID)-B.ID)INTO T FROM A A ,# B
WHERE SUBSTRING(','+A.F2,B.ID,1)=',' GROUP BY F1,F2,B.ID ORDER BY F1

ALTER FUNCTION UNION_HB(@A CHAR(10) )
RETURNS VARCHAR(8000)
AS 
BEGIN 
DECLARE @STR VARCHAR(1000)
SET @STR=''
SELECT @STR=@STR+','+F2 FROM T WHERE F1=@A
SET @STR=STUFF(@STR,1,1,'')
RETURN @STR
END 


SELECT DISTINCT CASE F1 WHEN '1'THEN 'A' WHEN '2' THEN 'B' ELSE 'C' END AS F1,DBO.UNION_HB(F1) AS F2 FROM T

[解决办法]
create table a(F1 varchar(100) , F2 varchar(100))
insert a select 'A', 'x,y,z' 
union all select 'A' ,'y,z,p' 
union all select 'B' ,'x,y' 
union all select 'B' ,'x,z' 
union all select 'C' ,'e,x' 


union all select 'C' ,'e,d' 
go
create function getAA(@f1 varchar(100))
returns varchar(1000)
as
begin
declare @sql varchar(1000)
select @sql=isnull(@sql+',','') +f2 from a where f1=@f1
return @sql
end
go
create function getBB(@f2 varchar(1000))
returns varchar(1000)
as
begin
declare @a table(id int identity(1,1),b int)
declare @b table(a varchar(1000),id int)
declare @sql varchar(1000)

insert @a select top 100 1 from syscolumns a ,syscolumns b
insert @b select substring(@f2+',',id,charindex(',',@f2+',',id)-id) x,id
from @a
where substring(','+@f2,id,1)=',' order by id
select @sql=isnull(@sql+',','')+a from (select * from @b b where not exists (select 1 from @b where a=b.a and id<b.id))bb 
return @sql
end
go
select f1, dbo.getBB(dbo.getAA(f1)) value from a group by f1