A字段    B字段            C字段  
101      现金              现金
10101    文一路校区        现金_文一路现金
10103    玉皇山校区        现金_玉皇山校区
10104    下沙校区          现金_下沙校区
1010401  下沙校区一组      现金_下沙校区_下沙校区一组
1010402  下沙校区二组      现金_下沙校区_下沙校区二组
1010403  下沙校区三组      现金_下沙校区_下沙校区三组
102      银行存款          银行存款
10201    工行              银行存款_工行




怎样写才能得到C字段这样的数据结果

解决方案 »

  1.   

    select a,b,ltrim(sys_connect_by_path(b,'-') ,'-') c
    from t
    start with length(a) = 3
    connect by  nocycle prior a =   substr(a,0,3)
      

  2.   


    同意,使用sys_connect_by_path来写这个。详细用户可以参考
    http://blog.csdn.net/inthirties/archive/2009/07/08/4331685.aspx
      

  3.   

    从事Java开发8年,并同时有5年的Oracle管理经验,有多年电信行业从业经验。能够短时间的快速故障,诊断分析定位和排查,擅长于系统性能优化,灾难数据恢复。
    :-),牛人呢。有时间到你的博踩踩
      

  4.   

    报prior不是有效的关系运算符?
      

  5.   

    ...
    你的oracle 版本是。
      

  6.   


    这个思路是对的,用树形结构和sys_connect_by_path来显示,
    但是处理上有些小问题, substr(a,0,3) 方法参数不对  用下面的试试。SQL>select * from sample_t1;
    SQL> select * from sample_t1;
    A                                        B
    ---------------------------------------- --------------------------------------------------------------------------------
    101                                      现金
    10101                                    文一路校区
    10103                                    玉皇山校区
    10104                                    下沙校区
    1010401                                  下沙校区一组
    1010402                                  下沙校区二组6 rows selected[code]SQL> col a format a10;
    SQL> col b format a20;
    SQL> with temp as
      2  (select a, b, decode(length(a), 3, '', substr(a, 1, length(a)-2)) p from sample_t1)
      3  select a, b, substr(sys_connect_by_path(b, '-'),2) c from temp start with p is null connect by prior a = p;
     
    A          B                    C
    ---------- -------------------- --------------------------------------------------------------------------------
    101        现金                 现金
    10101      文一路校区           现金-文一路校区
    10103      玉皇山校区           现金-玉皇山校区
    10104      下沙校区             现金-下沙校区
    1010401    下沙校区一组         现金-下沙校区-下沙校区一组
    1010402    下沙校区二组         现金-下沙校区-下沙校区二组
     
    6 rows selected[/code]
      

  7.   

    SQL> with temp as
      2  (select a, b, decode(length(a), 3, '', substr(a, 1, length(a)-2)) p from sample_t1)
      3  select a, b, substr(sys_connect_by_path(b, '-'),2) c from temp start with p is null connect by prior a = p;
     
    A          B                    C
    ---------- -------------------- --------------------------------------------------------------------------------
    101        现金                 现金
    10101      文一路校区           现金-文一路校区
    10103      玉皇山校区           现金-玉皇山校区
    10104      下沙校区             现金-下沙校区
    1010401    下沙校区一组         现金-下沙校区-下沙校区一组
    1010402    下沙校区二组         现金-下沙校区-下沙校区二组
     
    6 rows selected