日期:2014-05-17  浏览次数:20994 次

多个字段合并到一个字段...加急
SQL code
create table t_source_bj (库存编号 varchar2(10),名称 varchar2(20),零件备注码1 varchar2(20),零件备注码2 varchar2(20),零件备注码3 varchar2(20),零件备注码4 varchar2(20),合并项 varchar2(120));

insert into t_source_bj values('0704008','产品1','a','b','b','c','abbc');
insert into t_source_bj values('07043255','产品2','a','b','b','c','abbc');
insert into t_source_bj values('0702348','产品22','a','f','b','c','afbc');
insert into t_source_bj values('07432408','产品3','a','b','d','c','abdc');
insert into t_source_bj values('0723438','产品0','a','c','b','c','acbc');



这里希望合并项能够显示为:通过update或者其他方式,四个零件备注码的不重复合并,如第一行的合并项显示为:abc,最后一行显示为abc....其他行业是这样的。。。。。

------解决方案--------------------
SQL code

update t_source_bj A
set 合并项 =(
select 合并项 from
 (
  select 库存编号,replace(wm_concat(c),',','') 合并项 from (
    select distinct 库存编号,c from (
        select 库存编号,substr(合并项,rn,1) as c from
        t_source_bj  a ,(select level rn from dual connect by 1=1 and level <=4)b where length(a.合并项)>=b.rn )
  ) group by c
 ) B
where A.库存编号 = B.库存编号
)

------解决方案--------------------
SQL code

--表的别名有重复,不好意思,修改一下
update t_source_bj T_A
set 合并项 =(
select 合并项 from
 (
  select 库存编号,replace(wm_concat(c),',','') 合并项 from (
    select distinct 库存编号,c from (
        select 库存编号,substr(合并项,rn,1) as c from
        t_source_bj  a ,(select level rn from dual connect by 1=1 and level <=4)b where length(a.合并项)>=b.rn )
  ) group by c
 ) T_B
where T_A.库存编号 = T_B.库存编号