使用CONNECT BY PRIOR进行分层查询-Oracle SQL

时间:2019-09-05 13:30:36

标签: sql oracle recursion

我目前正在处理一项要求进行分层查询的需求,而这似乎是我无法理解的。

要求是: 对于给定的一组订单,找出所有要求,以及补充这些要求的内容。然后,在补货是MAKE类型(即另一张订单)的情况下,找出其所有要求和补货等。

Here is a dbfiddle containing all of the data and some example queries

小提琴结尾处的结果查询实际上是在说:对于订单x,这是其所有要求。对于这些需求中的每一个,这是计划对其进行补充的内容。

我现在要做的是为所有 make type 补货,我基本上需要继续进行此过程,方法是加入那些表,提取出要补货的东西,依此类推。第四,但是在跟踪顶级订单的同时这样做。

我希望将其转换为如下所示的数据集:

| Root Order | Order_Number | Requirement_ID | Replenishment_ID | Replenishment_Type | Replenishment_Detail | Replenishment_Date |
|:----------:|:------------:|:--------------:|:----------------:|:------------------:|:--------------------:|:------------------:|
|     300    |      300     |     AA-300     |       RO601      |       Bought       |          963         |      7/15/2018     |
|     300    |      300     |     AA-300     |       RO111      |        Make        |          251         |     10/23/2018     |
|     300    |      300     |     AA-300     |       RO435      |        Make        |          837         |      3/4/2018      |
|     300    |      300     |     AA-300     |       RO608      |        Make        |          850         |      4/27/2018     |
|     300    |      300     |     AA-516     |       RO734      |        Make        |          415         |      5/5/2018      |
|     300    |      300     |     AA-516     |       RO245      |       Bought       |          130         |      2/6/2018      |
|     300    |      300     |     AA-516     |       RO754      |       Bought       |          874         |      6/9/2018      |
|     300    |      300     |     AA-468     |       RO120      |        Make        |          333         |      7/28/2018     |
|     300    |      300     |     AA-468     |       RO96       |       Bought       |          279         |      6/11/2018     |
|     300    |      300     |     AA-744     |       RO576      |        Make        |          452         |      6/9/2018      |
|     300    |      300     |     AA-744     |       RO592      |       Bought       |          967         |      1/16/2018     |
|     300    |      300     |     AA-744     |       RO104      |        Make        |          232         |      1/30/2019     |
|     300    |      300     |     AA-744     |       RO169      |        Make        |          804         |      2/2/2018      |
|     300    |      130     |     AA-785     |       RO573      |        Make        |          616         |      4/1/2018      |
|     300    |      130     |     AA-785     |       RO139      |        Make        |          698         |      7/16/2018     |
|     300    |      130     |     AA-785     |       RO252      |        Make        |          190         |      8/2/2018      |
|     300    |      130     |     AA-785     |       RO561      |        Make        |          453         |      5/13/2018     |
|     300    |      130     |     AA-785     |       RO775      |        Make        |          974         |      8/7/2018      |
|     300    |      130     |     AA-171     |       RO92       |       Bought       |          493         |      4/1/2018      |
|     300    |      493     |     AA-400     |        RO4       |        Make        |          591         |      4/17/2018     |
|     300    |      493     |     AA-401     |       NULL       |        NULL        |         NULL         |        NULL        |
|     Now    |   Starting   |      From      |        The       |        Other       |                      |       Tables       |
|     300    |      591     |     AA-999     |        RO1       |       Bought       |          111         |      4/19/2019     |
|     300    |      591     |     AA-111     |        RO2       |       Bought       |          123         |      4/1/2019      |
|     300    |      591     |     AA-001     |       RO400      |        Make        |          124         |      5/1/2019      |
|     300    |      124     |     AA-313     |       RO112      |       Bought       |          102         |      7/8/2019      |
|     etc    |      etc     |       etc      |        etc       |         etc        |          etc         |         etc        |

在这里您可以看到订单300的补货量为130,然后补货493。

如何使用CONNECT_BY_ROOTCONNECT BY PRIOR实现这一目标?我已经尝试过像下面这样的递归WITH,但是不会产生层次结构。

WITH 
  rec(Root_Order, Order_Number, Requirement_ID, Replenishment_ID, Replenishment_Type, Replenishment_Detail, Replenishment_Date) AS (
  SELECT
    Orders.Order_Number AS Root_Order,
    Orders.Order_Number,
    Requirements.Requirement_ID,
    Replenishments.Replenishment_ID,
    Replenishments.Replenishment_Type,
    Replenishments.Replenishment_Detail,
    Replenishments.Replenishment_Date

  FROM
    Orders
      LEFT JOIN Requirements ON Orders.Order_Number = Requirements.Order_Number
      LEFT JOIN Lookup ON Requirements.Requirement_ID = Lookup.Requirement_ID
      LEFT JOIN Replenishments ON Lookup.Replenishment_ID = Replenishments.Replenishment_ID

UNION ALL

  SELECT
    rec.Order_Number
    rec.Replenishment_Details,
    Requirements.Requirement_ID,
    Replenishments.Replenishment_ID,
    Replenishments.Replenishment_Type,
    Replenishments.Replenishment_Detail,
    Replenishments.Replenishment_Date

  FROM
    rec
      LEFT JOIN Requirements ON Orders.Order_Number = Requirements.Order_Number
      LEFT JOIN Lookup ON Requirements.Requirement_ID = Lookup.Requirement_ID
      LEFT JOIN Replenishments ON Lookup.Replenishment_ID = Replenishments.Replenishment_ID
  )

  CYCLE Root_Order, Order_Number, Requirement_ID, Replenishment_ID, Replenishment_Type, Replenishment_Detail, Replenishment_Date SET CYCLE TO 1 DEFAULT 0

  SELECT DISTINCT * FROM rec

谢谢

2 个答案:

答案 0 :(得分:1)

从您的输入开始:

with data (
  Order_Number , Requirement_ID , Replenishment_ID ,
  Replenishment_Type , Replenishment_Detail , Replenishment_Date
) as (
  select 300,'AA-300','RO601' ,'Bought',  963, to_date('15-Jul-18','dd-Mon-rr') from dual union all    
  select 300,'AA-300','RO111' ,'Make',  251, to_date('23-Oct-18','dd-Mon-rr') from dual union all    
  select 300,'AA-300','RO435' ,'Make',  837, to_date('4-Mar-18','dd-Mon-rr') from dual union all    
  select 300,'AA-300','RO608' ,'Make',  850, to_date('27-Apr-18','dd-Mon-rr') from dual union all    
  select 300,'AA-516','RO734' ,'Make',  415, to_date('5-May-18','dd-Mon-rr') from dual union all    
  select 300,'AA-516','RO245' ,'Bought',  130, to_date('6-Feb-18','dd-Mon-rr') from dual union all    
  select 300,'AA-516','RO754' ,'Bought',  874, to_date('9-Jun-18','dd-Mon-rr') from dual union all    
  select 300,'AA-468','RO120' ,'Make',  333, to_date('28-Jul-18','dd-Mon-rr') from dual union all    
  select 300,'AA-468','RO96' ,'Bought',  279, to_date('11-Jun-18','dd-Mon-rr') from dual union all    
  select 300,'AA-744','RO576' ,'Make',  452, to_date('9-Jun-18','dd-Mon-rr') from dual union all    
  select 300,'AA-744','RO592' ,'Bought',  967, to_date('16-Jan-18','dd-Mon-rr') from dual union all    
  select 300,'AA-744','RO104' ,'Make',  232, to_date('30-Jan-19','dd-Mon-rr') from dual union all    
  select 300,'AA-744','RO169' ,'Make',  804, to_date('2-Feb-18','dd-Mon-rr') from dual union all    
  select 500,'AA-100','RO567' ,'Make',  725, to_date('22-Mar-18','dd-Mon-rr') from dual union all    
  select 500,'AA-100','RO90' ,'Bought',  240, to_date('14-Mar-18','dd-Mon-rr') from dual union all    
  select 500,'AA-100','RO202' ,'Bought',  185, to_date('26-Feb-18','dd-Mon-rr') from dual union all    
  select 500,'AA-823','RO764' ,'Bought',  629, to_date('15-Oct-18','dd-Mon-rr') from dual union all    
  select 500,'AA-823','RO434' ,'Make',  314, to_date('27-Jun-18','dd-Mon-rr') from dual union all    
  select 500,'AA-823','RO752' ,'Bought',  504, to_date('25-Apr-18','dd-Mon-rr') from dual union all    
  select 500,'AA-823','RO204' ,'Make',  847, to_date('9-Jul-18','dd-Mon-rr') from dual union all    
  select 500,'AA-239','RO367' ,'Bought',  652, to_date('14-Feb-18','dd-Mon-rr') from dual union all    
  select 500,'AA-239','RO732' ,'Bought',  561, to_date('3-Oct-18','dd-Mon-rr') from dual union all    
  select 130,'AA-785','RO573' ,'Make',  616, to_date('1-Apr-18','dd-Mon-rr') from dual union all    
  select 130,'AA-785','RO139' ,'Make',  698, to_date('16-Jul-18','dd-Mon-rr') from dual union all    
  select 130,'AA-785','RO252' ,'Make',  190, to_date('2-Aug-18','dd-Mon-rr') from dual union all    
  select 130,'AA-785','RO561' ,'Make',  453, to_date('13-May-18','dd-Mon-rr') from dual union all    
  select 130,'AA-785','RO775' ,'Make',  974, to_date('7-Aug-18','dd-Mon-rr') from dual union all    
  select 130,'AA-171','RO92' ,'Bought',  493, to_date('1-Apr-18','dd-Mon-rr') from dual union all    
  select 200,'AA-171','RO532' ,'Make',  727, to_date('17-May-18','dd-Mon-rr') from dual union all    
  select 200,'AA-337','RO29' ,'Make',  402, to_date('1-Jun-18','dd-Mon-rr') from dual union all    
  select 200,'AA-337','RO725' ,'Make',  892, to_date('9-Mar-18','dd-Mon-rr') from dual union all    
  select 200,'AA-533','RO216' ,'Bought',  637, to_date('1-Jun-18','dd-Mon-rr') from dual union all    
  select 100,'AA-100', NULL , NULL, NULL, NULL from dual union all    
  select 100,'AA-100','RO438' ,'Make',  125, to_date('19-Mar-18','dd-Mon-rr') from dual union all    
  select 493,'AA-400','RO4', 'Bought',  591, to_date('17-Apr-18','dd-Mon-rr') from dual union all    
  select 493,'AA-401', NULL , NULL, NULL, NULL from dual
)
select connect_by_root(order_number) root_order, data.*, level lvl
from data
start with order_number not in (
  select replenishment_detail from data where replenishment_detail is not null
)
connect by order_number = prior replenishment_detail
order siblings by order_number, replenishment_detail;

ROOT_ORDER ORDER_NUMBER REQUIR REPLE REPLEN REPLENISHMENT_DETAIL REPLENISHMENT_DATE         LVL
---------- ------------ ------ ----- ------ -------------------- ------------------- ----------
       100          100 AA-100 RO438 Make                    125 2018-03-19 00:00:00          1
       100          100 AA-100                                                                1
       200          200 AA-337 RO29  Make                    402 2018-06-01 00:00:00          1
       200          200 AA-533 RO216 Bought                  637 2018-06-01 00:00:00          1
       200          200 AA-171 RO532 Make                    727 2018-05-17 00:00:00          1
       200          200 AA-337 RO725 Make                    892 2018-03-09 00:00:00          1
       300          300 AA-516 RO245 Bought                  130 2018-02-06 00:00:00          1
       300          130 AA-785 RO252 Make                    190 2018-08-02 00:00:00          2
       300          130 AA-785 RO561 Make                    453 2018-05-13 00:00:00          2
       300          130 AA-171 RO92  Bought                  493 2018-04-01 00:00:00          2
       300          493 AA-400 RO4   Bought                  591 2018-04-17 00:00:00          3
       300          493 AA-401                                                                3
       300          130 AA-785 RO573 Make                    616 2018-04-01 00:00:00          2
       300          130 AA-785 RO139 Make                    698 2018-07-16 00:00:00          2
       300          130 AA-785 RO775 Make                    974 2018-08-07 00:00:00          2
       300          300 AA-744 RO104 Make                    232 2019-01-30 00:00:00          1
       300          300 AA-300 RO111 Make                    251 2018-10-23 00:00:00          1
       300          300 AA-468 RO96  Bought                  279 2018-06-11 00:00:00          1
       300          300 AA-468 RO120 Make                    333 2018-07-28 00:00:00          1
       300          300 AA-516 RO734 Make                    415 2018-05-05 00:00:00          1
       300          300 AA-744 RO576 Make                    452 2018-06-09 00:00:00          1
       300          300 AA-744 RO169 Make                    804 2018-02-02 00:00:00          1
       300          300 AA-300 RO435 Make                    837 2018-03-04 00:00:00          1
       300          300 AA-300 RO608 Make                    850 2018-04-27 00:00:00          1
       300          300 AA-516 RO754 Bought                  874 2018-06-09 00:00:00          1
       300          300 AA-300 RO601 Bought                  963 2018-07-15 00:00:00          1
       300          300 AA-744 RO592 Bought                  967 2018-01-16 00:00:00          1
       500          500 AA-100 RO202 Bought                  185 2018-02-26 00:00:00          1
       500          500 AA-100 RO90  Bought                  240 2018-03-14 00:00:00          1
       500          500 AA-823 RO434 Make                    314 2018-06-27 00:00:00          1
       500          500 AA-823 RO752 Bought                  504 2018-04-25 00:00:00          1
       500          500 AA-239 RO732 Bought                  561 2018-10-03 00:00:00          1
       500          500 AA-823 RO764 Bought                  629 2018-10-15 00:00:00          1
       500          500 AA-239 RO367 Bought                  652 2018-02-14 00:00:00          1
       500          500 AA-100 RO567 Make                    725 2018-03-22 00:00:00          1
       500          500 AA-823 RO204 Make                    847 2018-07-09 00:00:00          1

在WITH DATA子句中,替换您的联接。排序会将每个“根”顺序的所有行放在一起,但是在每个“根”中,层次结构将“深度优先”,因此您可以看到级别之间的直接关系。

最好的问候, 炖阿什顿

答案 1 :(得分:1)

我认为您正在寻找类似的东西:

with rec(root_order, order_number, requirement_id, replenishment_id, replenishment_type,
    replenishment_detail, replenishment_date)
as (
  -- anchor member
  select
    orders.order_number as root_order,
    orders.order_number,
    requirements.requirement_id,
    replenishments.replenishment_id,
    replenishments.replenishment_type,
    replenishments.replenishment_detail,
    replenishments.replenishment_date
  from orders
  join requirements on orders.order_number = requirements.order_number
  left join lookup on requirements.requirement_id = lookup.requirement_id
  left join replenishments on lookup.replenishment_id = replenishments.replenishment_id
  union all
  -- recursive member
  select rec.root_order,
    requirements.order_number,
    requirements.requirement_id,
    replenishments.replenishment_id,
    replenishments.replenishment_type,
    replenishments.replenishment_detail,
    replenishments.replenishment_date
  from rec
  join requirements on rec.replenishment_detail = requirements.order_number
  left join lookup on requirements.requirement_id = lookup.requirement_id
  left join replenishments on lookup.replenishment_id = replenishments.replenishment_id
)
select *
from rec
order by root_order, order_number, requirement_id;

锚成员本质上是您的原始查询,除了它添加了root_order,而且我使第一个联接成为一个内部查询以降低噪音(原始的87行中很多都只有{{ 1}},其他所有内容都为空)。

然后,递归成员将order_number(子订单号)加入rec.replenishment_detail,以沿层次结构走。不需要再次引用实际的订单表(除非您实际上需要从中获取其他字段,在这种情况下,将其包含在内很简单)。

您的示例数据可产生65行输出,包括:

requirements.order_number

db<>fiddle根据您的原始照片。

请注意,它也独立包含“子”订单:

ROOT_ORDER ORDER_NUMBER REQUIR REPLE REPLEN REPLENISHMENT_DETAIL REPLENISHM
---------- ------------ ------ ----- ------ -------------------- ----------
...
       300          130 AA-171 RO532 Make                    727 2018-05-17
       300          130 AA-171 RO92  Bought                  493 2018-04-01
       300          130 AA-785 RO573 Make                    616 2018-04-01
       300          130 AA-785 RO561 Make                    453 2018-05-13
       300          130 AA-785 RO775 Make                    974 2018-08-07
       300          130 AA-785 RO139 Make                    698 2018-07-16
       300          130 AA-785 RO252 Make                    190 2018-08-02
       300          300 AA-300 RO601 Bought                  963 2018-07-15
       300          300 AA-300 RO111 Make                    251 2018-10-23
       300          300 AA-300 RO435 Make                    837 2018-03-04
       300          300 AA-300 RO608 Make                    850 2018-04-27
       300          300 AA-468 RO96  Bought                  279 2018-06-11
       300          300 AA-468 RO120 Make                    333 2018-07-28
       300          300 AA-516 RO754 Bought                  874 2018-06-09
       300          300 AA-516 RO245 Bought                  130 2018-02-06
       300          300 AA-516 RO734 Make                    415 2018-05-05
       300          300 AA-744 RO169 Make                    804 2018-02-02
       300          300 AA-744 RO576 Make                    452 2018-06-09
       300          300 AA-744 RO592 Bought                  967 2018-01-16
       300          300 AA-744 RO104 Make                    232 2019-01-30
       300          493 AA-400 RO4   Bought                  591 2018-04-17
       300          493 AA-401                                             
...

等您可以从特定的目标顺序开始(例如,使锚成员具有... 130 130 AA-171 RO92 Bought 493 2018-04-01 130 130 AA-171 RO532 Make 727 2018-05-17 130 130 AA-785 RO775 Make 974 2018-08-07 130 130 AA-785 RO561 Make 453 2018-05-13 130 130 AA-785 RO252 Make 190 2018-08-02 130 130 AA-785 RO573 Make 616 2018-04-01 130 130 AA-785 RO139 Make 698 2018-07-16 130 493 AA-400 RO4 Bought 591 2018-04-17 130 493 AA-401 ... 493 493 AA-400 RO4 Bought 591 2018-04-17 493 493 AA-401 ... ),但尚不清楚这是否是您想要的。如果不是这样,并且您也不想自己查看较低的订单,那么您需要一种方法来识别顶级订单。一种方法是通过添加where orders.order_number = 300过滤器来排除显示为任何replenishment_detail值的所有订单:

not exists(...)

现在仅获得54行,并且不包括130/493 / etc。上面的根顺序行。

db<>fiddle


由于您实际上询问的是分层查询而不是递归查询,因此您可以执行以下操作:

with rec(root_order, order_number, requirement_id, replenishment_id, replenishment_type,
    replenishment_detail, replenishment_date)
as (
  -- anchor member
  select
    orders.order_number as root_order,
    orders.order_number,
    requirements.requirement_id,
    replenishments.replenishment_id,
    replenishments.replenishment_type,
    replenishments.replenishment_detail,
    replenishments.replenishment_date
  from orders
  join requirements on orders.order_number = requirements.order_number
  left join lookup on requirements.requirement_id = lookup.requirement_id
  left join replenishments on lookup.replenishment_id = replenishments.replenishment_id
  where not exists (
    select *
    from replenishments
    where replenishment_detail = orders.order_number
  )
  union all
  -- recursive member
  select rec.root_order,
    requirements.order_number,
    requirements.requirement_id,
    replenishments.replenishment_id,
    replenishments.replenishment_type,
    replenishments.replenishment_detail,
    replenishments.replenishment_date
  from rec
  join requirements on rec.replenishment_detail = requirements.order_number
  left join lookup on requirements.requirement_id = lookup.requirement_id
  left join replenishments on lookup.replenishment_id = replenishments.replenishment_id
)
select *
from rec
order by root_order, order_number, requirement_id;

CTE仍然是您的原始查询,它具有case表达式和exists子句,用于像以前一样确定每个订单是否为“根”订单,但现在作为标志而不是过滤器。然后,在其with cte (order_number, requirement_id, replenishment_id, replenishment_type, replenishment_detail, replenishment_date, is_root_order) as ( select orders.order_number, requirements.requirement_id, replenishments.replenishment_id, replenishments.replenishment_type, replenishments.replenishment_detail, replenishments.replenishment_date, case when exists ( select * from replenishments where replenishment_detail = orders.order_number ) then 'N' else 'Y' end from orders join requirements on orders.order_number = requirements.order_number left join lookup on requirements.requirement_id = lookup.requirement_id left join replenishments on lookup.replenishment_id = replenishments.replenishment_id ) select connect_by_root(order_number) as root_order, order_number, requirement_id, replenishment_id, replenishment_type, replenishment_detail, replenishment_date from cte start with is_root_order = 'Y' connect by order_number = prior replenishment_detail; 子句中使用该标志,分层查询就非常简单。

还有另一个db<>fiddle

(我刚刚意识到,这几乎是@StewAshton所说的;我的CTE本质上是他的“替代您的联接”步骤。唯一的真正不同是他将标志计算直接移到了{{ 1}}子句,实际上它的效率可能更高,因为它不必再次击中starts with表...)

我通常更喜欢递归CTE方法,但是层次结构方法在此仅出于其简洁性就很有吸引力。不过,您可能想将这两种方法的性能与实际数据进行比较。