0

以下是我的桌子

PARTY
ID, PARTY_ID, PARENT_ID

PARTY_PARTY
BUYER_ID, SUPPLIER_ID

PARTY表包含买方和供应商的记录。买方将有一个或多个儿童购买者(由儿童购买者列引用PARENT_ID),这些儿童购买者可以依次拥有儿童购买者。

每个买家也可以有一个或多个供应商,并且在PARTY_PARTY表格中跟踪此关联,其中BUYER_ID列保存买家 ID,SUPPLIER_ID列保存他的供应商 ID。

我能够像这样获得给定买家的所有买家和供应商

获取给定买家的所有分层买家

SELECT ID FROM party START WITH party_id = 'BUYERX' CONNECT BY PRIOR ID = parent_id;

获取上述买家的所有供应商

SELECT ID
        FROM party
        WHERE ID IN (
                SELECT SUPPLIER_ID
                FROM party_party
                WHERE BUYER_ID IN (
                        SELECT ID
                        FROM party START WITH party_id = 'BUYERX' CONNECT BY PRIOR ID = parent_id
                        )
                )

但这里的问题是——最终的供应商可以充当另一家供应商的买方。在那种情况下,我怎样才能获得所有买家和供应商,其中买家包括作为买家的买家和供应商。

我不想使用任何视图或程序!

样本数据

PARTY
ID, PARTY_ID, PARENT_ID
1, BX, NULL
2, BY, 1
3, BZ, 1
4, SA, NULL
5, SB, NULL
6, SC, NULL
7, SD, NULL
8, SE, NULL
9, SF, NULL

PARTY_PARTY
BUYER_ID, SUPPLIER_ID
1, 4
2, 5
3, 6
6, 7
6, 8
8, 9
4

2 回答 2

0

如果您提供示例数据会更容易。如果我正确理解了买家、供应商和家长之间的关系,你可以试试这个解决方案:

WITH
  party AS (
          SELECT 1 AS id, 'BUYERX' AS party_id, NULL AS parent_id FROM dual
    UNION SELECT 2 AS id, 'B02' AS party_id, 1 AS parent_id FROM dual
    UNION SELECT 3 AS id, 'B03' AS party_id, 1 AS parent_id FROM dual
    UNION SELECT 4 AS id, 'B04' AS party_id, 2 AS parent_id FROM dual
    UNION SELECT 5 AS id, 'B05' AS party_id, 3 AS parent_id FROM dual
  ),
  party_party AS (
          SELECT 2 AS buyer_id, 5 AS supplier_id FROM dual
    UNION SELECT 1 AS buyer_id, 4 AS supplier_id FROM dual
  )
SELECT
    id, LPAD('  ', (level - 1) * 2) || party_id AS party_id, parent_id, level
  FROM (
    SELECT id, party_id, parent_id
      FROM party
    UNION ALL
    SELECT supplier_id AS id, 'Supplier' AS party_id, buyer_id AS parent_id
      FROM party_party
  )
START WITH party_id = 'BUYERX'
CONNECT BY PRIOR id = parent_id
;

输出:

   ID PARTY_ID PARENT_ID LEVEL
----- -------------- --------- -----
    1 买家 1
    2 B02 1 2
    4 B04 2 3
    5 供应商 2 3
    3 B03 1 2
    5 B05 3 3
    4 供应商 1 2

编辑您的示例数据,我的查询返回此输出:

ID PARTY_ID PARENT_ID LEVEL
---- ------ --------- -----
 1 盒 1
 2 乘 1 2
 5 供应商 2 3
 3 BZ 1 2
 6 供应商 3 3
 7 供应商 6 4
 8 供应商 6 4
 9 供应商 8 5
 4 供应商 1 2
于 2013-10-25T08:28:01.770 回答
0

这是另一种方法,没有 CONNECT BY:

  WITH   
  party AS (  
          SELECT 1 AS id, 'BUYERX' AS party_id NULL AS parent_id FROM dual  
    UNION SELECT 2 AS id, 'B02' AS party_id, 1 AS parent_id FROM dual  
    UNION SELECT 3 AS id, 'B03' AS party_id, 1 AS parent_id FROM dual  
    UNION SELECT 4 AS id, 'B04' AS party_id, 2 AS parent_id FROM dual  
    UNION SELECT 5 AS id, 'B05' AS party_id, 3 AS parent_id FROM dual  
  ),  
  party_party AS (  
          SELECT 2 AS buyer_id, 5 AS supplier_id FROM dual  
    UNION SELECT 1 AS buyer_id, 4 AS supplier_id FROM dual  
  ),  
  merged(id,party_id,parent_id)as(  
    SELECT id, party_id, parent_id  
      FROM party  
    UNION ALL  
    SELECT supplier_id AS id, 'Supplier' AS party_id, buyer_id AS parent_id  
      FROM party_party),  
  grouped(par,node,clevel,client)as(  
  select parent_id,id,1,party_id from merged where parent_id is null union all  
  select c.parent_id,c.id,s.clevel+1,lpad(' ',4*s.clevel)||c.party_id  
  from merged c  
  join grouped s on s.node=c.parent_id)  
  select * from grouped order by par nulls first,clevel,node
于 2013-10-25T09:41:31.930 回答