测试数据
编号 物料类别 产品编号
1 铅笔 0001
2 铅笔 0002
3 铅笔 0001
4 钢笔 0004
5 钢笔 0005
6 钢笔 0004
7 圆珠笔 0007
8 圆珠笔 0008
9 圆珠笔 0007等等要求实现以下效果:
编号 物料类别 产品编号
1 铅笔 0001,0002
2 钢笔 0004,0005
3 圆珠笔 0007,0008**我的目的是如果不重复则合并产品编号,如果重复则舍弃**请问如何实现!!!!!???
编号 物料类别 产品编号
1 铅笔 0001
2 铅笔 0002
3 铅笔 0001
4 钢笔 0004
5 钢笔 0005
6 钢笔 0004
7 圆珠笔 0007
8 圆珠笔 0008
9 圆珠笔 0007等等要求实现以下效果:
编号 物料类别 产品编号
1 铅笔 0001,0002
2 钢笔 0004,0005
3 圆珠笔 0007,0008**我的目的是如果不重复则合并产品编号,如果重复则舍弃**请问如何实现!!!!!???
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+ cast(產品編號 as varchar(100)) from 表A where 物料类别=@Item
set @str=right(@str,len(@str)-1)
return(@str)
End =========
調用:
select distinct 物料类别,dbo.str_merg(物料类别) from 表A結果包含了重復的編號:
物料类别 产品编号
铅笔 0001,0002,0001
钢笔 0004,0005,0004
圆珠笔 0007,0008,0007如何舍棄這些重復的編號呢?
insert T select 1, '铅笔', '0001'
union all select 2, '铅笔', '0002'
union all select 3, '铅笔', '0001'
union all select 4, '钢笔', '0004'
union all select 5, '钢笔', '0005'
union all select 6, '钢笔', '0004'
union all select 7, '圆珠笔', '0007'
union all select 8, '圆珠笔', '0008'
union all select 9, '圆珠笔', '0007'create function fun(@物料类别 nvarchar(10))
returns nvarchar(200)
as
begin
declare @re nvarchar(200)
set @re=''
select @re=@re+产品编号+',' from T where 物料类别=@物料类别 group by 产品编号 select @re=left(@re, len(@re)-1)
return @re
endselect distinct 物料类别, dbo.fun(物料类别) from T--result
物料类别
---------- -------------------------------
钢笔 0004 ,0005
铅笔 0001 ,0002
圆珠笔 0007 ,0008 (3 row(s) affected)