有一张表A格式为id  code     price
1  100,101,   50
2  102,103,   20
3  104,       30我现在想要
id  code     price
1  100       50
1  101       50
2  102       20
2  103       20
3  104       30谢谢!

解决方案 »

  1.   

    楼上的意思,就是:
    列转行:
    列:code列,以逗号分隔有两个部分, 要转成两行,每行的code值都是逗号间的值。
      

  2.   


    with t as (
    select 1 id, '100,101,' code, 50 price from dual union
    select 2, '102,103,', 20 from dual union
    select 3, '104,', 30 from dual 
    )
    select t.id,rtrim(regexp_substr(code,'[0-9]+,',1,rn),',') code2
    from t,(select rownum rn from dual connect by level < 100) r
    where regexp_substr(code,'[0-9]+,',1,rn) is not null
    order by t.id,code2
    -- 其中level < 100这里的100是取你code字段的最大分割次数,如果超出100你可以把这个值上调。
      

  3.   

    --手上没有oracle环境,随便写了下。
    with t as (
    select 1 id, '100,101,' code, 50 price from dual union
    select 2, '102,103,', 20 from dual union
    select 3, '104,', 30 from dual 
    )
    select t.id,regexp_substr(code,'[^,]+',1,l)
    from t,(select level l from dual connect by level < 10) r --这里的10表示最多有10-1=9个逗号分隔。如果不过自行改正。
    where length(code)-length(replace(code,',',''))+1>r.l
    order by t.id,1;
      

  4.   

    太厉害了,没有环境随便写的SQL也没错误。