oracle某字段值能否转成记录里的列?

比如我有个表,记录是
字段名   a          b           c
对应值   12       3           45;78;9;
用什么样的sql能获得这样的记录:

12  3   45
12  3   78
12 3     9
其中c字段是用;分隔的不定长记录,请大家看看啊!!
我也来说两句 查看全部回复

最新回复

  • redneck_he (2008-9-18 18:11:53)

    sql> create table demo (c1 varchar2(4), c2 varchar2(4),c3 varchar2(30));

    SQL> insert into demo values(12,3,'45;78;9;');

    1 row inserted

    SQL> commit  ;

    Commit complete

    SQL> select c1,c2,substr(c3,
                       decode(rownum,1,1,instr(c3,';',1,rownum-1)+1),
                       instr(c3,';',1,rownum)-decode(rownum,1,1,instr(c3,';',1,rownum-1)+1)) c3
           from demo connect by rownum <= length(c3) - length(replace(c3,';',''));

    C1   C2   C3
    ---- ---- ----------------------------------------
    12   3    45
    12   3    78
    12   3    9
  • redneck_he (2008-9-18 18:14:18)

    对于n多记录的可能不太适用,仅仅是个范例
  • shaozb (2008-9-22 16:19:43)

    如果值是规律的、有限的,可以使用枚举处理如下:

    select decode(a,12) as col_a,decode(b,3) as col_b,
         case when instr(c,'45;')>0 then
                 45
                 when instr(c,'78;')>0 then
                 78
                 when instr(c,'9;')>0 then
                 9
                 ...
                 else
                 ...
                 end  as col_c
    from tab ;
  • shaozb (2008-9-22 17:04:14)

    Sorry ! 看错了