Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The length of the trade chain is equal to the depth of the recursion. Oracle has a psuedo variable, LEVEL, that you can use. If you take your original query and sort by LEVEL desending the end of the longest chain will be first. Then we need to eliminate the rest of the query. <p><pre> select * from ( select o.OFFERED_PRODUCT_ID, o.DEMANDED_PRODUCT_ID from TRADES o start with o.DEMANDED_PRODUCT_ID = (SELECT MIN(o.DEMANDED_PRODUCT_ID) from TRADES) connect by NOCYCLE prior o.DEMANDED_PRODUCT_ID = o.OFFERED_PRODUCT_ID order by level desc ) where rownum = 1; </pre><code><p> The above will return the last row in the chain. To get the entire chain in a comma seperated list: <p><pre> select lvl, opath ||','|| DEMANDED_PRODUCT_ID chain from ( select level lvl, o.OFFERED_PRODUCT_ID, o.DEMANDED_PRODUCT_ID, SYS_CONNECT_BY_PATH(o.OFFERED_PRODUCT_ID, ',') opath from TRADES o start with o.DEMANDED_PRODUCT_ID = (SELECT MIN(o.DEMANDED_PRODUCT_ID) from TRADES)connect by NOCYCLE prior o.DEMANDED_PRODUCT_ID = o.OFFERED_PRODUCT_ID order by level desc ) where rownum = 1 </pre></code><p>You may want to remove the leading comma. <p>If you want the longest chain, one row at a time:<p><pre> select c.OFFERED_PRODUCT_ID, c.DEMANDED_PRODUCT_ID from TRADES c start with (c.OFFERED_PRODUCT_ID, c.DEMANDED_PRODUCT_ID) in ( select o1, d1 from ( select level lvl, o.OFFERED_PRODUCT_ID, o.DEMANDED_PRODUCT_ID , CONNECT_BY_ROOT OFFERED_PRODUCT_ID o1 , CONNECT_BY_ROOT DEMANDED_PRODUCT_ID d1 from TRADES o start with o.OFFERED_PRODUCT_ID not in (select st.DEMANDED_PRODUCT_ID from trades st) connect by NOCYCLE prior o.DEMANDED_PRODUCT_ID = o.OFFERED_PRODUCT_ID order by level desc ) where rownum = 1 ) connect by NOCYCLE prior c.DEMANDED_PRODUCT_ID = c.OFFERED_PRODUCT_ID </pre><p> The <code>CONNECT_BY_ROOT</code> command gets data from the root of the recursion, i.e., the first row of the chain. I'm running on 10g Rel 2. I'm not sure if it's available on older versions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload