表结构(customer):
cust_id,cust_name,cust_type,cust_parent,cust_level
表中已经按如下排序了结果:
cust_id      cust_name   cust_type   cust_parent   cust_level
001            广东       01         null           1
001002         深圳       02         001            2
001002001      南山       03         001002         3
001002002      福田       03         001002         3
001003         东莞       02         001            2
001004         广州       02         001            2
001005        万家乐      03         001            2
...........................................................................
我使用了oracle 家族树查询代码如下:
select lpad('  ',*(cust_level-1)||cust_name,  cust_type ,  cust_parent,   cust_level
from customer
start with cust_parent is null 
connect by prior cust_id=cust_parent
出来的结果是:
广东           01         null           1
    深圳       02         001            2
        南山   03         001002         3   
        福田   03         001002         3
    东莞       02         001            2
    万家乐     03         001            2
    广州       02         001            2
与我希望的结果不同。我已经按cust_type排序的呀。。希望的结果是如下才是对呀。。
广东           01         null           1
    深圳       02         001            2
        南山   03         001002         3   
        福田   03         001002         3
    东莞       02         001            2
    万家乐     03         001            2
    广州       02         001            2我试了多种方法都没有正确的结果,请兄弟们指点。

解决方案 »

  1.   

    表结构(customer):
    cust_id,cust_name,cust_type,cust_parent,cust_level
    表中已经按如下排序了结果:
    cust_id      cust_name   cust_type   cust_parent   cust_level
    001            广东       01         null           1
    001002         深圳       02         001            2
    001002001      南山       03         001002         3
    001002002      福田       03         001002         3
    001003         东莞       02         001            2
    001004         广州       02         001            2
    001005        万家乐      03         001            2
    ...........................................................................
    我使用了oracle 家族树查询代码如下:
    select lpad('  ',*(cust_level-1)||cust_name,  cust_type ,  cust_parent,   cust_level
    from customer
    start with cust_parent
    connect by prior cust_id=cust_parent
    出来的结果是:
    广东           01         null           1
        深圳       02         001            2
            南山   03         001002         3   
            福田   03         001002         3
        东莞       02         001            2
        万家乐     03         001            2
        广州       02         001            2
    与我希望的结果不同。我已经按cust_type排序的呀。。希望的结果是如下才是对呀。。
    广东           01         null           1
        深圳       02         001            2
            南山   03         001002         3   
            福田   03         001002         3
        东莞       02         001            2
        广州       02         001            2
        万家乐     03         001            2
        我试了多种方法都没有正确的结果,请兄弟们指点。昨晚太晚了。。把结果发错了。。
      

  2.   

    cust_level是冗余数据,oracle本身就有这个伪列,所以应该是
    select lpad('  ',(level-1)*2) ||cust_name,  cust_type ,  cust_parent,   cust_level
    from customer
    start with cust_id = '001' 
    connect by cust_parent=prior cust_id;
      

  3.   

    还是没有能解决问题。。主要是利用oracle 家族树查询时没有按照原表的顺序展开..
    如 ‘广州’是一级代理广东下的二级代理商,在表中的顺序在一级代理广东下的零销商‘万家乐’之前,但oracle 家族树查询后的结果‘广州’确在‘ 万家乐’之后。。不知道如何设置才可以使‘广州’确在‘ 万家乐’之前(只后sql语句)。。
      

  4.   

    ???
    12:55:10 SQL> select lpad(cust_name,(cust_level-1)*4+lengthb(cust_name),' ') cus
    t_name,cust_type,cust_parent,cust_level
    12:55:12   2  from customer
    12:55:12   3  start with cust_parent is null connect by prior cust_id=cust_paren
    t;CUST_NAME            CU CUST_PARENT     CUST_LEVEL
    -------------------- -- --------------- ----------
    广东                 01                          1
        深圳             02 001                      2
            南山         03 001002                   3
            福田         03 001002                   3
        东莞             02 001                      2
        广州             02 001                      2
        万家乐           03 001                      2已选择7行。已用时间:  00: 00: 00.00