oracle中的递归关系

时间:2016-01-12 01:32:46

标签: sql oracle

我有一张表格,用于存储客户,相关客户和相关客户的数量。 相关客户可以再次拥有更多相关客户,就像递归关系一样。

我想找出一位客户的所有相关客户(直到最后一位相关客户)。

表统计: -

目前的音量是2,00,000条记录。

Create table
--------------
CREATE TABLE RELATED_TABLE 
( CUS_ID            VARCHAR2(09) ,
  REL_CUS_ID        VARCHAR2(09) ,
  COUNT_OF_REL_CUST NUMBER(12)) ;

Sample data:-
---------------  
 INSERT INTO RELATED_TABLE VALUES ('402758970','898196448',3);
 INSERT INTO RELATED_TABLE VALUES ('402758970','855115206',3);
 INSERT INTO RELATED_TABLE VALUES ('402758970','850353774',3);
 INSERT INTO RELATED_TABLE VALUES ('898196448','691094946',3);
 INSERT INTO RELATED_TABLE VALUES ('898196448','404636299',3);
 INSERT INTO RELATED_TABLE VALUES ('898196448','402758970',3);
 INSERT INTO RELATED_TABLE VALUES ('855115206','870397045',3);
 INSERT INTO RELATED_TABLE VALUES ('855115206','855115206',3);
 INSERT INTO RELATED_TABLE VALUES ('855115206','402758970',3);


CUS_ID     REL_CUS_ID    COUNT_OF_REL_CUST   
402758970  898196448       3                                                          
402758970  855115206       3                                                          
402758970  850353774       3                                     
898196448  691094946       3                                     
898196448  404636299       3                                     
898196448  402758970       3                                            
855115206  870397045       3                                            
855115206  855115206       3                                            
855115206  402758970       3

OUTPUT:-
--------------------

402758970  898196448
402758970  855115206
402758970  850353774
402758970  691094946
402758970  404636299
402758970  870397045
402758970  855115206

修改

我曾经使用过这样的东西,但是在用户数据中出现了错误

select * from
  (select connect_by_root(cus_id) cus_id, rel_cus_id
     from RELATED_TABLE
       start with rel_cus_id is not null
       connect by prior cus_id = rel_cus_id)
  where cus_id <> rel_cus_id

2 个答案:

答案 0 :(得分:0)

以下查询会返回您的预期结果:

 with tree ( cust_id, child_id, path, exist) as
 (select cus_id ,
         rel_cus_id ,
         cus_id || ',' || rel_cus_id,
         0
   from  RELATED_TABLE 
  where cus_id = '402758970'
  union all
   select t.cust_id,
          rt.rel_cus_id,
          t.path || ',' || rt.rel_cus_id,
          instr(t.path, rt.cus_id)
   from tree t
   join related_table rt
    on t.child_id = rt.cus_id and
        t.exist = 0 )
    select distinct cust_id, child_id from tree

我用exist来减少周期。 distinct用于删除不同路径接收的重复。

SQL Fiddle

中的示例

如果您不想显示402758970 -> 402758970关系,请使用以下内容:

 with tree ( cust_id, child_id, path) as
 (select cus_id ,
         rel_cus_id ,
         cus_id || ',' || rel_cus_id
   from  RELATED_TABLE 
  where cus_id = '402758970'
  union all
   select t.cust_id,
          rt.rel_cus_id,
          t.path || ',' || rt.rel_cus_id
   from tree t
   join related_table rt
    on t.child_id = rt.cus_id and
          instr(t.path, rt.rel_cus_id) = 0 )
    select distinct cust_id, child_id from tree

答案 1 :(得分:0)

我能够使用下面的SQL

获取输出
SQL> SELECT  DISTINCT '402758970' CUS_ID,
  2                   REL_CUS_ID
  3    FROM  RELATED_TABLE
  4    START WITH CUS_ID = '402758970'
  5    CONNECT BY NOCYCLE PRIOR REL_CUS_ID = CUS_ID
  6  /

CUS_ID    REL_CUS_ID
--------- ---------
402758970 898196448
402758970 691094946
402758970 870397045
402758970 402758970
402758970 404636299
402758970 850353774
402758970 855115206