Hierarchical Query from a leaf node

I have thist table
create table employee(
emp_id number(7) not null
name varchar(30)
supervisor_id number(7)
Constraint PK_employee primary key(emp_id))
This is my data
Insert into employee(emp_id,name) values (1,Boss)
Insert into employee values(2,Subordinate1,1)
Insert into employee values(3,Subordinate2,1)
Insert into employee values(4,Subordinate3,1)
Insert into employee values(5,Subordinate4,2)
Insert into employee values(6,Subordinate5,5)
Insert into employee values(7,Subordinate6,3)
In my program, I'll be given the leaf node lets say, Subordinate5, I want to get the whole hierarchy for that subordinate like this:
Boss\Subordinate1\Subordinate4\Subordinate5
or Subordinate 6
Boss\Subordinate2\Subordinate6
Help will be greatly appreciated. Thanks.

Here is another - a little bit rewritten - version:
michaels>  SELECT full_path
  FROM (SELECT LTRIM (SYS_CONNECT_BY_PATH (NAME, '\'), '\') AS full_path
              FROM employee e
        CONNECT BY PRIOR emp_id = supervisor_id ORDER BY LEVEL DESC)
WHERE ROWNUM = 1 AND SUBSTR (full_path, INSTR (full_path, '\', -1) + 1) = 'Subordinate5'
FULL_PATH                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
Boss\Subordinate1\Subordinate4\Subordinate5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
1 row selected.

Similar Messages

  • Hierarchical Dimension with a leaf node w/o any parent

    Hi
    We have a Product Dimension with five levels L0, L1, L2, L3 and L4 (L0-lowestlevel, L4-HighestLevel).
    With one Hierarchy STANDARD as
    L4, NULL (Parent)
    L3, L4 (L4 is the parent of L3)
    L2, L3
    L1, L2
    L0, L1 (L1 is the parent of L0)
    The product dimension table has a record with values only for L0 and NULL values for other Levels..
    (ie) We have a requirement to create a value in L0 which will have no parents.. The rest of the values in L0 will have parents.
    Would like to know if this is possible in AW. This was possible in Express Server
    regards
    uma

    HI
    yes we have run into a big problem. We have created leaf nodes w/o parent in aw and created relational views of those. The measure views thus created has data for these dimension values but when accessed thru BI Beans (ie in the cross-tab if we have this measure) the data is not getting displayed for these dimension values (Which do not have parent)
    Any immediate response will be of greate help to us.

  • Hierarchical Query from 4 tables

    Hi,
    I'm using database 10.2.0.1.0.
    I have 4 tables . The data
    SQL> select * from table1;
          TT1
            1
            2
            3
            4Where tt1 is a number column
    SQL> Select * from table2;
          TT1 TT2
            1 x
            1 y
            2 a
            3 testing search
    SQL> Select * from table3;
          TT1 TT2                            TT3
            1 x                              xy
            1 x                              xz
            1 y                              yn
            2 a                              ax
    SQL> Select * from table4;
          TT1 TT2        TT3        TT4
            1 x          xy         testing for the width expansion
            1 x          xz         testing
            1 y          yn         ynx
            2 a          ax         axy
            2 a          ax         axzFrom these table i want to write a hierarchichal query to populate a tree , such that the tree looks like
    1
       -- x
           --  testing for the width expansion
           --  testing
       -- y
           -- ynx
    2
       -- a
           -- axy
           -- axz
    3
       -- testing search
    4Note: Table3's data is not shown in the tree, but is used indirectly to get the data from table4.
    Please help
    Thanks

    Yes, that's the script.
    Sample output for you:
    SQL> SELECT LPAD(' ', 2*level) || granted_role "USER PRIVS"
    FROM (
    SELECT NULL grantee, username granted_role
    FROM dba_users
    WHERE username LIKE UPPER('system')
    UNION
    SELECT grantee, granted_role
    FROM dba_role_privs
    UNION
    SELECT grantee, privilege
    FROM dba_sys_privs)
    START WITH grantee IS NULL
    CONNECT BY grantee = prior granted_role;
    USER PRIVS
      SYSTEM
        AQ_ADMINISTRATOR_ROLE
          CREATE EVALUATION CONTEXT
          CREATE RULE
          CREATE RULE SET
          DEQUEUE ANY QUEUE
          ENQUEUE ANY QUEUE
          MANAGE ANY QUEUE
        CREATE MATERIALIZED VIEWWould this work?
    //Johan

  • Drill and Navifgate only from leaf node

    Hi
    I have implemented level based hierarchy for my dimension. I want to navigate to another report only from the leaf node of the hierarchy.
    Can you please let me know how can this be achieved? How can i identify the leaf node in a level based hierarchy

    This is not a duplicate of the item mentioned. With one I have a node anywhere in the tree and need to walk up to find the parent. In this one, I have the parent and need to delete leaf nodes up to this parent. I each one I have a different set of constraints and different ids to work with. Thanks for your help.

  • Hierarchical Query with Rollup Sum (CONNECT BY with GROUP BY ROLLUP)

    Hi all,
    Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
    CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
    CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
    INSERT INTO ACCOUNTS VALUES ('TOT',NULL,'Total');
    INSERT INTO ACCOUNTS VALUES ('ANA1','TOT','General Expenses');
    INSERT INTO ACCOUNTS VALUES ('4801001','ANA1','Small Expenses');
    INSERT INTO ACCOUNTS VALUES ('4801002','ANA1','Transportation');
    INSERT INTO ACCOUNTS VALUES ('ANA2','TOT','Health Expenses');
    INSERT INTO ACCOUNTS VALUES ('4802001','ANA2','Healthcare');
    INSERT INTO ACCOUNTS VALUES ('4802002','ANA2','Facilities');
    INSERT INTO BALANCES VALUES ('4801001', 2000);
    INSERT INTO BALANCES VALUES ('4801002', 1000);
    INSERT INTO BALANCES VALUES ('4802001', 3000);
    INSERT INTO BALANCES VALUES ('4802002', 4000);What i need in this scenario is to run a hierarchical query, where for each node i compute the sum of all its children (In LEAF nodes which are the child accounts, this sum is the value in BALANCES itself). Final Result would be:
    TOT -> 10000
      ANA1 -> 3000
        4801001 -> 2000
        4801001 -> 1000
      ANA2 -> 7000
        4802001 -> 3000
        4802002 -> 4000I have tried various ways, and found out a workaround which works for a fixed amount of levels, basically it builds the hierarchy and computes the SYS_CONNECT_BY_PATH, then splits this as a regular expression and uses GROUP BY ROLLUP to compute the higher levels. Then i assemble it again, now with the computed values. Below is the example query:
    select level
        , NVL (vfinal.child_account,'TOTAL') ||' - '||
                            ( SELECT account_desc
                                FROM accounts
                               WHERE account_code = vfinal.child_acct ) account_name
         , to_char(sum_bal, 'fm999g999g999g990') as rolled_up_balance
      from
    select coalesce( princ.lvl3, princ.lvl2, princ.lvl1 ) child_acct
         , DECODE ( princ.lvl2 , NULL
                                     , NULL
                                     , DECODE ( princ.conta_lvl3, NULL
                                     , princ.conta_lvl1,princ.conta_lvl2 ) ) parent_acct
         , sum(princ.balance_amount) sum_bal
    from (
    select hier.lvl1
         , hier.lvl2
         , hier.lvl3
         , hier.parent_account
         , hier.account_code child_acc
         , bal.balance_amount
      from ( select level 
                  , sys_connect_by_path( account_code, '/' ) hierarchy_acct
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,3) lvl3
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,2) lvl2
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,1) lvl1
                  , account_code
                  , parent_account 
               from accounts acc
               where level <= 3
               start with parent_account is null
               connect by nocycle prior account = parent_account
               order siblings by parent_account
               ) hier
          , balances  bal
      where bal.cod_conta  = hier.account_code
    ) princ
    where princ.lvl1 is not null
    group by rollup ( princ.lvl1
                    , princ.lvl2
                    , princ.lvl3 )
    order by princ.conta_lvl1
           , princ.conta_lvl2
           , princ.conta_lvl3
    ) vfinal
    where child_acct is not null
    start with parent_acct is null
    connect by nocycle prior child_acct = parent_acctAll said and done, what i need is to do the same thing for infinite levels, because this query has 3 fixed levels. Do you know how can i structure a new query where, independently of the number of levels, the parent sums are all rolled up like this?
    Thanks a lot in advance! Best Regards!
    Thiago
    Edited by: Thiago on Sep 6, 2011 11:31 AM
    Edited by: Thiago on Sep 6, 2011 1:01 PM

    Hi,
    Thiago wrote:
    Hi all,
    Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
    CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
    CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
    INSERT INTO ACCOUNTS ('TOT',NULL,'Total');
    INSERT INTO ACCOUNTS ('ANA1','TOT','General Expenses');
    INSERT INTO ACCOUNTS ('4801001','ANA1','Small Expenses');
    INSERT INTO ACCOUNTS ('4801002','ANA1','Transportation');
    INSERT INTO ACCOUNTS ('ANA2','TOT','Health Expenses');
    INSERT INTO ACCOUNTS ('4802001','ANA2','Healthcare');
    INSERT INTO ACCOUNTS ('4802002','ANA2','Facilities');
    INSERT INTO BALANCES ('4801001', 2000);
    INSERT INTO BALANCES ('4801001', 1000);
    INSERT INTO BALANCES ('4802001', 3000);
    INSERT INTO BALANCES ('4802001', 4000);
    Thanks for posting the CREATE TABLE and INSERT statements. Remember why you do it: so that the people who want to help you can re-create the problem and test their ideas. If the statments don't work, then they are not so useful. None of the INSERT statements you posted work: they all need a VALUES keyword. Please test those statments before you post them.
    Also, make sure that the reuslts you post correspond to the sample data you post. In your sample data, there are no rows in balances for account_codes '4801002' or '4802002'.
    I think you want something like this:
    WITH  connect_by_results      AS
         SELECT     CONNECT_BY_ROOT account_code     AS root_account_code
         ,     account_code
         FROM     accounts
                             -- NOTE: No START WITH clause
         CONNECT BY     parent_account     = PRIOR account_code
    SELECT       c.root_account_code     || ' -> '
                          || TO_CHAR (SUM (b.balance_amount))     AS txt
    FROM           connect_by_results  c
    LEFT OUTER JOIN      balances          b  ON  c.account_code = b.account_code
    GROUP BY  c.root_account_code
    ;

  • Help on building up a Hierarchical Query

    Hello
    I have a requirement to build the approval group based on invoice amount on the invoice line in payable. For Example if the invoice line amount is 10000k then I want to send the invoice for approval till level 3 approval authority from the requester on the invoice line.. I have created a query as below and pas.person_id I am passing 62 (this is the person id of the requester on the invoice_line and approval starts from requester). By using this query i am getting the full approval hierarchical query from requester to CEO but i don't know how to restrict the records to fetch till approval hierarchy 3, in addition in case if I don't have any person for approval authority 3 then i should pick next immediate higher authority person (in this case 4)....Note: current requester don't have any approval authority and in case if the requester itself has approval authority 3 then  query should only return requester record.
    Can anybody help me on this?
    select
    z.person_id,z.full_name,z.job_id, z.level1, pj.name,pj.approval_authority
    from  PER_JOBS_VL pj,
    (select distinct pp.person_id,pp.full_name, pas.job_id,LEVEL level1 from per_all_people_f pp,
      per_all_assignments_f pas
    where pp.person_id= pas.person_id
    start with pas.person_id = 62
    connect by prior  pas.supervisor_id = pp.person_id
    and trunc(sysdate) between trunc(pas.effective_start_date) and trunc(pas.effective_end_date)
    and trunc(sysdate) between trunc(pas.effective_start_date) and trunc(pas.effective_end_date)
    order by LEVEL )z
    where pj.job_id = z.job_id
    order by 4
    PERSON_ID,FULL_NAME,JOB_ID,LEVEL1,JOB_NAME,APPROVAL_AUTHORITY
    62,      aaa,       64,     1,     Buyer,
    63,      bbb,       66,      2,    Director,  4
    121,    ccc,       66,      3,    Director,  4
    68,      ddd,      1296,   4,    CFO,       7
    71,      eee,      1259,   5,    CEO,       7
    Thanks!

    Hi
    Now I am trying to join more table to your query I am getting error. Can you please help..
    Query I got from you
    SELECT    person_id, full_name, job_id, level1, name, approval_authority
    FROM
    (    SELECT pp.person_id,
               pp.full_name,
               pas.job_id,
               LEVEL AS level1,
               pj.name,
               pj.approval_authority,
               ROW_NUMBER ()
                  OVER (PARTITION BY pj.approval_authority ORDER BY ROWNUM)
                  AS r_num
          FROM per_jobs_vl pj
               JOIN per_all_assignments_f pas
                  ON pas.job_id = pj.job_id
               JOIN per_all_people_f pp
                  ON pp.person_id = pas.person_id
    where 1=1
    and trunc(sysdate) between trunc(pas.effective_start_date) and trunc(pas.effective_end_date)
    and trunc(sysdate) between trunc(pp.effective_start_date) and trunc(pp.effective_end_date)          
    START WITH pas.person_id = 62
    CONNECT BY PRIOR pas.supervisor_id = pp.person_id
               AND NVL (PRIOR pj.approval_authority, 0) <
                      :approval_authority_limit )
    where r_num    = 1
    ORDER BY  level1     
    I modified to include more tables
    SELECT    ail.invoice_id,ail.line_number,ail.amount,
    person_id, full_name, job_id, level1, name, approval_authority
    FROM
    ap_invoice_lines_all ail, ap_invoice_distributions_all aid,
    (    SELECT
    --            ail.invoice_id,
      --          ail.line_number,
                pp.person_id,
               pp.full_name,
               pas.job_id,
               LEVEL AS level1,
               pj.name,
               pj.approval_authority,
               ROW_NUMBER ()
                  OVER (PARTITION BY pj.approval_authority ORDER BY ROWNUM)
                  AS r_num
          FROM per_jobs_vl pj,
                  --ap_invoice_lines_all_temp ail1, ap_invoice_distributions_all_temp aid1,
                per_all_assignments_f pas,
                per_all_people_f pp
    where 1=1
    and pp.person_id = pas.person_id
    and pas.job_id = pj.job_id
    --and ail1.invoice_id = aid1.invoice_id
    --and ail1.line_number = aid1.invoice_line_number
    --and ail1.invoice_id = ail.invoice_id
    ---and ail1.line_number = ail.line_number
    --and pp.person_id = ail.REQUESTER_ID --62
    --and ail.invoice_id = 46044 and aid.invoice_line_number=1
    and trunc(sysdate) between trunc(pas.effective_start_date) and trunc(pas.effective_end_date)
    and trunc(sysdate) between trunc(pp.effective_start_date) and trunc(pp.effective_end_date)           
    START WITH pas.person_id = 62--ail.REQUESTER_ID --62
    --(select requeter_id from ap_invoice_lines_all_temp where invoice_id = ail.invoice_id and line_number = ail.line_number)
    CONNECT BY PRIOR pas.supervisor_id = pp.person_id
               AND NVL (PRIOR pj.approval_authority, 0) <
                      :approval_authority_limit )
    where r_num    = 1
    and ail.invoice_id = aid.invoice_id
    and ail.line_number = aid.invoice_line_number
    and ail.amount >0
    --and pp.person_id = ail.REQUESTER_ID --62
    and ail.invoice_id = 46044 --and aid.invoice_line_number=1
    ORDER BY  2,level1 
    CREATE TABLE AP_INVOICE_LINES_ALL_TEMP
      INVOICE_ID                      NUMBER(15)    NOT NULL,
      LINE_NUMBER                     NUMBER        NOT NULL,
      LINE_TYPE_LOOKUP_CODE           VARCHAR2(25 BYTE) NOT NULL,
      REQUESTER_ID                    NUMBER(15),
      ORG_ID                          NUMBER(15)    DEFAULT NULL,
      AMOUNT                          NUMBER
    SET DEFINE OFF;
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, REQUESTER_ID, ORG_ID, AMOUNT)
    Values
       (46044, 1, 'ITEM', 62, 84,
        2500);
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, REQUESTER_ID, ORG_ID, AMOUNT)
    Values
       (46044, 2, 'MISCELLANEOUS', 6035, 84,
        300);
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, REQUESTER_ID, ORG_ID, AMOUNT)
    Values
       (46044, 3, 'FREIGHT', 1632, 84,
        200);
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, ORG_ID, AMOUNT)
    Values
       (46044, 4, 'TAX', 84, 0);
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, ORG_ID, AMOUNT)
    Values
       (46044, 5, 'TAX', 84, 0);
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, ORG_ID, AMOUNT)
    Values
       (46044, 6, 'ITEM', 84, 10000.02);
    Insert into AP_INVOICE_LINES_ALL_TEMP
       (INVOICE_ID, LINE_NUMBER, LINE_TYPE_LOOKUP_CODE, REQUESTER_ID, ORG_ID, AMOUNT)
    Values
       (46044, 7, 'ITEM', 6035, 84,
        25000.01);
    COMMIT;
    CREATE TABLE fnd_lookup_values_vl_temp
      LOOKUP_TYPE  VARCHAR2(25 BYTE) NOT NULL,
      ATTRIBUTE1                      VARCHAR2(25 BYTE) NOT NULL,
      ATTRIBUTE2                      VARCHAR2(25 BYTE) NOT NULL,
      ATTRIBUTE3                      VARCHAR2(25 BYTE) NOT NULL,
      ATTRIBUTE4                      VARCHAR2(25 BYTE) NOT NULL,
      ATTRIBUTE5                      VARCHAR2(25 BYTE) NOT NULL
    SET DEFINE OFF;
    Insert into APPS.fnd_lookup_values_vl_temp
       (LOOKUP_TYPE, ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3, ATTRIBUTE4, ATTRIBUTE5)
    Values
       ('DISTR_APRV_LVL', '84', '0', '10000', 'Inventory',
        '3');
    Insert into APPS.fnd_lookup_values_vl_temp
       (LOOKUP_TYPE, ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3, ATTRIBUTE4, ATTRIBUTE5)
    Values
       ('DISTR_APRV_LVL', '84', '10000.01', '25000.00', 'Inventory',
        '4');
    Insert into APPS.fnd_lookup_values_vl_temp
       (LOOKUP_TYPE, ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3, ATTRIBUTE4, ATTRIBUTE5)
    Values
       ('DISTR_APRV_LVL', '84', '25000.01', '50000.00', 'Inventory',
        '5');
    COMMIT;
    approval_authority_limit will be retried from the below query for each line (based on invoice line amount the approval authority will fetched from the below query)
    select
    flv.attribute5 from  ap_invoice_lines_all_temp ail,
    fnd_lookup_values_vl_temp flv
    where
    flv.lookup_type = 'DISTR_APRV_LVL'
    and flv.attribute1 = ail.org_id
    and ail.amount between flv.attribute2 and flv.attribute3
    and ail.invoice_id = 46044
    --and ail.line_number=1
    requester_id will be retried from the below query for each line
    select requester_id from ap_invoice_lines_all_temp
    where invoice_id = 46044
    --and line_number = 1
    My Expected results should be as follow (for line 1 and 7)
    Invoice_id             Line_Number      Amount            person_id            job_id        level      Name   Apporval_authority
    46044                           1                  2500                62                       64            1       Buyer             
    46044                           1                  2500                63                       66            2       Director                4
    46044                           7                  25000.01          62                       64            1       Buyer             
    46044                           7                  25000.01          63                       66            2       Director                4
    46044                           7                  25000.01          68                       1296         3      CFO                      7
    Please help on this...
    Thanks!

  • Hierarchical query - Stop at specific leaf nodes - How to in Oracle 9i ?

    Table H -- Master table to build Hierarchical tree
    C -- Child
    P -- Parent
    Table RN -- Table defining Root Nodes
    N -- Node
    Table LN -- Table defining Leaf Nodes
    N -- Node
    The following Query can generate trees starting with the nodes specified in the Table:RN
    SELECT LEVEL L, C, P, SYS_CONNECT_BY_PATH(C,'/') SCBP
    FROM H
    START WITH C IN ( SELECT N FROM RN )
    CONNECT BY PRIOR C = P
    How do I limit the tree to the nodes specified in the LN table ?
    "CONNECT BY" does not support "IN" clause
    i.e
    CONNECT BY PRIOR C = P AND P NOT IN (SELECT N FROM LN)
    Say we have 2 trees
    1-2-3-4-5
    A-B-C-D-E
    RN : 2,B
    LN : 5,D
    Result:
    2,3,4 (5 is excluded)
    B,C (D is excluded)
    Any help is appreciated...

    What about:
    select level l, c, p, sys_connect_by_path(c,'/') scbp
      from (select * from h
             where c not in (select n from ln))  -- filter via an inline view
    start with c in ( select n from rn )
    connect by prior c = p;

  • Leaf Node access in a BW OLAP Universe - BEX Query

    How we can gain access in an universe to just the lowest leaf node in an bex query with hierarchies.  If an infoobject has a  hierarchy that is ragged, I just want to display that lowest level with a value.
    Is there an MDX work around
    An example is cost center or account which may be many levels. 
    The goal is to create a universe such that in webi or explorer, the user has to deal with one field for cost center or account and not 7 levels for each which make it very hard for the end user.
    In this case the hierarchy is not important.

    Hi,
    I assume that you want to say XML traces rather than SQL traces.
    We provide SQL for relational databases and SAP BW Infocubes/DSO through Data Federator.
    The MDX generated contains only the characteristics and key figures selected in the Query Panel.
    However, there were an issue where all key figures were taken into account by SAP BW server even if not all have been selected.
    This issue has been fixed in a fix pack (I don't remember which one).
    Regards
    Didier

  • [Oracle 8i] Need help pruning branches from a hierarchical query

    My problem is that my hierarchical query seems only to trim out the values that don't meet my criteria, but still includes their children. When my query hits a record that does not meet my criteria, I want it to stop there. I've tried including the criteria in just the 'where' clause of the query, and have also put the criteria in the 'connect by' clause as well, but nothing has fixed it. Please keep in mind I'm using Oracle 8i, so I can't use some of the 'nicer' statements for hierarchical queries that they introduced in 9. I'm stuck with 'Start With...Connect By'.
    I have sample tables/data that I can post if someone needs to see that to help me, but to start with, here's my current query:
    SELECT     *
    FROM     (
              SELECT
                   LEVEL
              ,     c_bill.comp_part_nbr                     AS     c_part_nbr
              ,     (select c_part.part_desc
                   FROM part c_part
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)     AS     c_part_desc
              ,     (SELECT c_part.part_type
                   FROM part c_part
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_part_type
              ,     c_bill.qty_per                          AS     c_qty_per_p
              ,     c_bill.qty_per_type                     AS     c_qty_per_type
              ,     (SELECT c_part.qty_on_hand                
                   FROM part c_part
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_qty_on_hand
              ,     c_bill.oper_nbr                     AS     rqd_at_op
              ,     c_bill.comp_off_adj                     AS     rqd_offset
              ,     c_bill.bom_doc_nbr                     AS     p_part_nbr
              ,     (SELECT p_part.qty_on_hand
                   FROM part p_part
                   WHERE p_part.part_nbr=c_bill.bom_doc_nbr)      AS     p_qty_on_hand
              FROM
                   BILL c_bill
              WHERE
                             (c_bill.status           =      'RL')           
                        AND     (c_bill.view_code     IN      ('M','G'))     
                        AND     (c_bill.end_eff_dt     >      SYSDATE)      
                        AND     (c_bill.begn_eff_dt     <=      SYSDATE)
              START WITH c_bill.bom_doc_nbr=RPAD(?,25)
              CONNECT BY PRIOR c_bill.comp_part_nbr=c_bill.bom_doc_nbr
              AND     c_bill.view_code     IN     ('M','G')     
              AND     c_bill.status          =     'RL'
              AND      c_bill.end_eff_dt     >     SYSDATE
              AND     c_bill.begn_eff_dt     <=     SYSDATE     
         ) a
    WHERE     c_part_type = 'M'

    The outside criterion of part_type='M' isn't my problem. Where I'm actually seeing my issue rear its ugly head is in the criterion:
    (c_bill.view_code     IN      ('M','G'))What I'll have happen is that one of the children or grandchildren of the part number I'm querying for (my parameter), will be of some view code that's not 'M' or 'G'. In my sample data below, I have a level 4 part that is part of the 'H' view code, which I don't want, nor do I want it's children. However, its child is in the 'G' view code, and my query returns it anyway.
    In my sample data below, I'm assuming that the parameter = 'XYZ-100'
    CREATE TABLE part
    part_nbr     varchar(25) not null,
    part_desc     varchar(25) not null,
    part_type     char(1) not null,
    qty_on_hand     double(13,4) not null
    CONSTRAINT part_pk
    PRIMARY KEY (part_nbr),
    CONSTRAINT check_part_type
    CHECK (part_type IN ('M','P','X','Y')),
    CONSTRAINT check_qty_on_hand
    CHECK (qty_on_hand >= 0)
    CREATE TABLE bill
    row_added_ts     char(20) not null,
    bom_doc_nbr     varchar(25) not null,
    comp_part_nbr     varchar(25) not null,
    qty_per          double(9,5) not null,
    qty_per_type     char(1) not null,
    oper_nbr     char(4) not null,
    comp_off_adj     double(3,0),
    status          char(2),
    view_code     char(1) not null,
    end_eff_dt     date() not null,
    begn_eff_dt     date() not null
    CONSTRAINT bill_pk
    PRIMARY KEY (row_added_ts),
    CONSTRAINT check_qty_per_type
    CHECK (qty_per_type IN ('0','1','2','3')),
    CONSTRAINT check_status
    CHECK (status IN ('IN', 'RL')),
    );     Values for those tables:
    INSERT INTO part
    VALUES ('xyz-1', 'purchased part', 'P', 5);
    INSERT INTO part
    VALUES ('xyz-2', 'purchased part', 'P', 1);
    INSERT INTO part
    VALUES ('xyz-3', 'purchased part', 'P', 1);
    INSERT INTO part
    VALUES ('xyz-3a', 'manufactured part', 'M', 1);
    INSERT INTO part
    VALUES ('xyz-4', 'purchased part', 'P', 1);
    INSERT INTO part
    VALUES ('xyz-9-1', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('xyz-9a', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('raw-1', 'purchased raw material', 'P', 212);
    INSERT INTO part
    VALUES ('raw-2', 'purchased raw material', 'P', 75.5);
    INSERT INTO part
    VALUES ('XYZ-100', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('(OPEN)', '(not in use)', 'Y', 0);
    INSERT INTO part
    VALUES ('XYZ-100-1', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('XYZ-100-2', 'manufactured part', 'M', 1);
    INSERT INTO part
    VALUES ('XYZ-100-3', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('XYZ-100-4', 'manufactured part', 'M', 2);
    INSERT INTO part
    VALUES ('XYZ-100-A', 'manufactured part', 'M', 0);
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100','xyz-1',3,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100','XYZ-100-1',1,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-1','xyz-1',2,'1','****',1,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-1','XYZ-100-2',3,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-2','xyz-2',6,'1','****',2,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-2','xyz-4',6,'1','****',2,'IN','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-2','xyz-100-3',1,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','xyz-3',8,'1','****',1,'RL','M','01-Jan-2050','01-Jan-2000');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','xyz-3a',8,'1','****',1,'RL','M','01-Jan-2000','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','XYZ-100-4',4,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','XYZ-100-A',2,'1','****',2,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008071153100150000','XYZ-100-3','(OPEN)',2,'1','****',0,'RL','E','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008071153100150000','XYZ-100-3','xyz-9-1',2,'1','****',0,'RL','H','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-4','raw-1',8.75,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-A','raw-2',3.75,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008075911100150000','xyz-9-1','xyz-9a',1,'1','****',0,'RL','G','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008087711100150000','xyz-9a','raw-2',3.75,'1','****',0,'RL','G','01-Jan-2050','01-Jan-1900');Sample data displayed in table format:
    --PART table (from insert statements above)
    part_nbr     part_desc          part_type     qty_on_hand
    xyz-1           purchased part          P          5
    xyz-2           purchased part          P          1
    xyz-3           purchased part          P          1
    xyz-3a           manufactured part     M          1
    xyz-4           purchased part          P          1
    xyz-9-1           manufactured part     M          0
    xyz-9a           manufactured part     M          0
    raw-1           purchased raw material     P          212
    raw-2           purchased raw material     P          75.5
    XYZ-100           manufactured part     M          0
    (OPEN)          (not in use)          Y          0
    XYZ-100-1     manufactured part     M          0
    XYZ-100-2     manufactured part     M          1
    XYZ-100-3     manufactured part     M          0
    XYZ-100-4     manufactured part     M          2
    XYZ-100-A     manufactured part     M          0
    --BILL table (from insert statements above)
    row_added_ts          bom_doc_nbr     comp_part_nbr     qty_per     qty_per_type     oper_nbr     comp_off_adj     status     view_code     end_eff_dt     begn_eff_dt
    2008072153100150000     XYZ-100          xyz-1          3     1          ****          0          RL     G          01-Jan-2050     01-Jan-1900
    2008072223100150000     XYZ-100          XYZ-100-1     1     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008072411100150000     XYZ-100-1     xyz-1          2     1          ****          1          RL     M          01-Jan-2050     01-Jan-1900
    2008072459100150000     XYZ-100-1     XYZ-100-2     3     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008072578100150000     XYZ-100-2     xyz-2          6     1          ****          2          RL     M          01-Jan-2050     01-Jan-1900
    2008072694100150000     XYZ-100-2     xyz-4          6     1          ****          2          IN     G          01-Jan-2050     01-Jan-1900
    2008072786100150000     XYZ-100-2     xyz-100-3     1     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008072865100150000     XYZ-100-3     xyz-3          8     1          ****          1          RL     M          01-Jan-2050     01-Jan-2000
    2008073100100150000     XYZ-100-3     xyz-3a          8     1          ****          1          RL     M          01-Jan-2000     01-Jan-1900
    2008073159100150000     XYZ-100-3     XYZ-100-4     4     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008073346100150000     XYZ-100-3     XYZ-100-A     2     1          ****          2          RL     M          01-Jan-2050     01-Jan-1900
    2008073478100150000     XYZ-100-3     (OPEN)          2     1          ****          0          RL     E          01-Jan-2050     01-Jan-1900
    2008073529100150000     XYZ-100-3     xyz-9-1          2     1          ****          0          RL     H          01-Jan-2050     01-Jan-1900
    2008073798100150000     XYZ-100-4     raw-1          8.75     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008073811100150000     XYZ-100-A     raw-2          3.75     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008075911100150000     xyz-9-1          xyz-9a          1     1          ****          0          RL     G          01-Jan-2050     01-Jan-1900
    2008087711100150000     xyz-9a          raw-2          3.75     1          ****          0          RL     G          01-Jan-2050     01-Jan-1900--What I want to get with my query (branches pruned off my tree)
    LEVEL     C_PART_NBR     C_PART_DESC          C_PART_TYPE     C_QTY_PER_P     C_QTY_PER_TYPE     C_QTY_ON_HAND     RQD_AT_OP     RQD_OFFSET     P_PART_NBR     P_QTY_ON_HAND
    1     XYZ-100-1     manufactured part     M          1          1          0          ****          0          XYZ-100          0
    2     XYZ-100-2     manufactured part     M          3          1          1          ****          0          XYZ-100-1     0
    3     xyz-100-3     manufactured part     M          1          1          0          ****          0          XYZ-100-2     1
    4     XYZ-100-4     manufactured part     M          4          1          2          ****          0          XYZ-100-3     0
    4     XYZ-100-A     manufactured part     M          2          1          0          ****          2          XYZ-100-3     0--What I actually get with my query (includes children of items that don't meet query criteria)
    LEVEL     C_PART_NBR     C_PART_DESC          C_PART_TYPE     C_QTY_PER_P     C_QTY_PER_TYPE     C_QTY_ON_HAND     RQD_AT_OP     RQD_OFFSET     P_PART_NBR     P_QTY_ON_HAND
    1     XYZ-100-1     manufactured part     M          1          1          0          ****          0          XYZ-100          0
    2     XYZ-100-2     manufactured part     M          3          1          1          ****          0          XYZ-100-1     0
    3     xyz-100-3     manufactured part     M          1          1          0          ****          0          XYZ-100-2     1
    4     XYZ-100-4     manufactured part     M          4          1          2          ****          0          XYZ-100-3     0
    4     XYZ-100-A     manufactured part     M          2          1          0          ****          2          XYZ-100-3     0
    5     xyz-9a          manufactured part     M          1          1          0          ****          0          xyz-9-1          0Edited by: user11033437 on Jul 30, 2009 7:27 AM (grammar)

  • Hide leaf nodes from jtree.

    Hi,
    How can I hide all nodes that are leafs on a tree? Basically, I'm creating a splitpane where on the left side I have a jtree where when a user clicks on a node on that jtree the leaf nodes will appear on the right split pane as icons.
    V

    Thanks for the reply...
    I wanted to include the leaf nodes in the tree model so that I won't need to work with two data structures--one with the leaf nodes, and another without the leaf nodes.
    So far, I think I resolved the problem. I created my own treemodel class where I pass a defaulttreemodel into it and created a modified getChildCount which will go into the defaulttreemodel and filter out the leaf nodes.
    It all seems to work pretty well.
    My problem now... How do I remove the stupid node icons? :)
    I just want the dashed lines to appear and nothing more....
    Thanks again...
    V

  • Hiding leaf node from jtree ?

    Hi,
    How can I hide all nodes that are leafs on a tree?
    Basically, I'm creating a splitpane where on the left side I have a jtree where when a user clicks on a node ( as a folder ) on that jtree the leaf nodes ( all the children nodes of the selected node ) will appear on the right split pane as icons.
    thanks in advance!

    Define your own renderer class eg:
    class NavTreeCellRenderer extends DefaultTreeCellRenderer {
    public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean sel,
    boolean expanded,
    boolean leaf,
    int row,
    boolean hasFocus)
    if(leaf == true){
    this.setPreferredSize(new Dimension(0, 0));
    super.getTreeCellRendererComponent(tree, dto.getNodeLabel(), sel, expanded, true, row, hasFocus);
    return this;
    Then set this rederer to ur tree
    NavTreeCellRenderer navTCR = new NavTreeCellRenderer();
    jTree.setCellRenderer(navTCR);
    And then ENJOY

  • Hierarchical query with many-to-many relationship

    I have read with interest the creative solutions to complex hierarchical queries posted previously; they have been instructive but have not quite addressed this scenario.
    We have a hierarchy table H, with columns for ID, name, parentID, and other attributes.
    Within this table are a number of independent hierarchies, each existing for a different purpose.
    We have a master list of hierarchies in table T which describes the purpose of each hierarchy, provides some default attributes which the nodes can inherit, and stores a unique id for each hierarchy and a pointer to the root node of the corresponding hierarchy in table H.
    We have a master list of items M, with identically named columns to those in H, along with many other attributes.
    The members of table M ALL belong to EACH of the Hierarchies. So we have a link table I to define the intersection of H and M.
    So the leaf nodes of H are really containers for the list of elements from M which may be attached to them.
    The universe of M is very volatile, with new members being added, old ones deleted, and existing ones being reclassified frequently from node to node within each hierarchy. Since the hierarchies have to be built to handle every possible scenario, so that the members of M can always find a suitable node to reside in, quite often, in fact more often than not, the majority of leaf nodes for each hierarchy are empty at any given moment.
    Therefore, although we always know the root sector of a given hierarchy and can traverse downwards from there, if we worked our way up from the intersection table, we could eliminate up to 70% of the nodes of any given hierarchy from further consideration, as they don't need to be (in fact, must not be) included in reports.
    As implied by the above, rows in M are structurally similar (in terms of columns, but not in any real world sense) and are a superset of rows in H. But combining them into the one table doesn't seem to help the reporting process due to the many-to-many relationship which prevents the ID/parentID relationship from being carried through to this level.
    There are a number of other considerations of which the most pertinent is that the people using this database generally have an interest in only a subset of the master list of items in M. This relationship is also dynamic but important enough and rigid enough that another link table P exists to combine the Users in table U with the subset of M in which they are interested. (The users are also grouped into hierarchies of a totally different nature, but this aspect is secondary for now.)
    The reporting is reasonably straightforward for any single combination of User and Hierarchy; they want to see all the items they are interested in, listed in hierarchical sequence, totalled on change of level with the individual items M listed beneath the nodes of H. This is unfortunately required in real time, so retrieval performance is paramount.
    Some statistics might help to determine the optimum approach:
    The largest hierarchy has 10,000 nodes. The smallest about 100.
    The largest would have 70% or more of its nodes unused at any point in time, and even the smallest would have 25% unused.
    The hierarchies tend to be broad rather than deep, the maximum number of levels being about 5; but the larger ones should be twice as deep as this if performance was not compromised.
    There are dozens of hierarchies, but it may be possible to sharply reduce this number by exploiting the Order Siblings By clause.
    The number of rows in M varies between 500,000 and 50,000; depending upon how long historical data is retained on-line (and performance permitting, it would be retained indefinitely).
    The number of users varies between 1000 and 2000 but the range of M in which they are interested varies greatly; from as few as 100 to as many as 10,000+. So it is almost always worth beginning by eliminating the items in which they are not interested, implying once again that the hierarchy should be traversed upwards rather than down from the root.
    The current system is very old and survives by a tactic of building what are essentially materialised views of the database structure for each user overnight using, ahem, non-relational technology. This is inefficient and not easily scaled (but it works) and hence this redevelopment project needs to (a) work, and (b) work better and faster.
    I am happy to provide some DDL scripts if that helps explain the problem better than this narrative.
    I can't help feeling that the solution lies in somehow extending the hierarchical query past the many-to-many link table so that the Master list can be merged directly into the hierarchy such that the M items become the leaf nodes rather than the design outlined above - but I don't know how to do that. But I am sure everyone reading this does! :)
    All advice appreciated. Database version is not an issue; we are currently using version 10XE for experimentation, but production usage could be on 11 if that contains helpful features.
    Thank you
    CS

    Hi,
    ChrisS. wrote:
    I am happy to provide some DDL scripts if that helps explain the problem better than this narrative.Yes, please do.
    The problem seems interesting, I'm sure many people here (including myself) are willing to help you in this matter.
    So yes, post DDL for the tables, as well as INSERTs to populate them with representative data. Please also include the output you require along with detailed explanations about the logic to get it.
    Don't forget to put lines of code between &#x007B;code&#x007D; tags in order to preserve formatting and readability, like this :
    SELECT sysdate FROM dual;Thanks.

  • Too many results in hierarchically query

    Hello all,
    I'm searching for an idea to stop getting results 2, 3 and more times out of the following query
    select
    t.lvl,
    t.syswflvl,
    t.upper,
    LPAD(' ', (lvl)*8)||t.code code,
    LPAD(' ', (lvl)*8)||t.bezeichnung bezeichnung,
    t.chk,
    t.rang
    from (
    select '0' lvl, '0-'||to_char(syswftable,'0000000') syswflvl, '0- 0000000' upper, syscode code, bezeichnung, '' chk, 0 rang from wftable where (select count(wfm.syswftable) from wfm where wfm.syswftable = wftable.syswftable) > 0 union
    select '1' lvl, '1-'||to_char(syswfm,'0000000') syswflvl, '0-'||to_char(syswftable,'0000000') upper, syscode code, kurzbez bezeichnung, anzeigefilter chk, 1 rang from wfm union
    select '2' lvl, '2-'||to_char(syswfa,'0000000') syswflvl, '1-'||to_char(syswfm,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfa union
    select '3' lvl, '3-'||to_char(syswfc,'0000000') syswflvl, '2-'||to_char(syswfa,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfc union
    select '4' lvl, '4-'||to_char(syswfg,'0000000') syswflvl, '3-'||to_char(syswfc,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfg
    ) t
    where ((t.chk not like '%and 0%'
    and trim(t.chk) not like '0%')
    or t.chk is null)
    and upper not like '%-'
    connect by nocycle prior syswflvl = upper
    order siblings by upper, syswflvl, rang
    What happens is, that I get the results from level 0 one times, from level 1 two times, from level 2 three times etc.
    What I'm try to achive is to get only the whole thing once.
    Hope you can see what my problem is ;-)
    Regards
    Carsten

    The effect of not having a start with clause in a hierarchical query is that a hierarchy is produced starting at every possible entry-point. So, you'll get the hierachy from node 1, the hierarchy from all nodes 2, the hierarchy from all nodes 3 and so one. Whether it starts with the correct one or not is not really relevant, as you already figured out that you get too much....
    So, just believe it, add a start with clause...and your problems are gone.

  • How to make the leaf node of the APEX tree downloadable

    Hi All,
    I am trying to build a "library" page for my application, with the documents as the leaf nodes of the tree. The documents come from a database table, and each document is a BLOB.
    My question is, how should I write the "link" part of the APEX tree query to make the lead node document downloadable for the end users?
    Thanks,
    Christine

    Hilary helped me out of this by creating a new form page with two new items there corresponding to the PK and BLOB column, then in the tree query use apex_util.get_blob_file_src function as the link. Below is the email from Hilary:
    1. Created Form, page 13, based upon your table storing the documents as BLOBs. The form page has just two associated items: P13_DOC and P13_DOC_ID. The item P13_DOC is of type FILE and contains a source type of DB column, which is the first required parameters of the function get_blob_file_src. NOTE: I could have created a form region on the same page as your tree, but chose to generate a separate page. You may choose to change this yourself.
    2. Updated the Tree query on pg 12 to use the following link for tree nodes of level 4:
    apex_util.get_blob_file_src('P13_DOC',v.attr3)
    ...where P13_DOC is the application page item mentioned in step 1 above, and v.attr3 should hold the unique ID associated with the document. Your tree now allows users to download the listed documents.

  • Hierarchical query to combine two groupings into one broad joint grouping

    Hi there,
    I would like to know if anyone knows a way to solve the problem below with a SQL querie, maybe using some hierarchical queries or window functions (or anything else in SQL for that matter).
    My environment is:
    Oracle Database 11g Release 11.2.0.2.0 - 64bit
    The problem is this:
    I have a list of items that are grouped together in two different grouping ways (two columns).
    This gives the ability for items to be linked to other items in two ways:
    1. Directly if both have same value on GROUP1 and/or GROUP2;
    2. indirectly if they have an item in common with at least one match on either GROUP1 or GROUP2.
    The idea is to start from this dataset:
    WITH T AS
      SELECT 1 AS ITEM_ID, 'A' AS GROUP1, 100 AS GROUP2 FROM DUAL UNION
      SELECT 2 AS ITEM_ID, 'A' AS GROUP1, 100 AS GROUP2 FROM DUAL UNION
      SELECT 3 AS ITEM_ID, 'A' AS GROUP1, 101 AS GROUP2 FROM DUAL UNION
      SELECT 4 AS ITEM_ID, 'B' AS GROUP1, 100 AS GROUP2 FROM DUAL UNION
      SELECT 5 AS ITEM_ID, 'B' AS GROUP1, 102 AS GROUP2 FROM DUAL UNION
      SELECT 6 AS ITEM_ID, 'C' AS GROUP1, 103 AS GROUP2 FROM DUAL UNION
      SELECT 7 AS ITEM_ID, 'D' AS GROUP1, 101 AS GROUP2 FROM DUAL
    SELECT * FROM T;
    And end up with this dataset with a one single joint grouping:
    WITH T AS
      SELECT 1000 AS JOINT_GROUP_ID, 1 AS ITEM_ID FROM DUAL UNION
      SELECT 1000 AS JOINT_GROUP_ID, 2 AS ITEM_ID FROM DUAL UNION
      SELECT 1000 AS JOINT_GROUP_ID, 3 AS ITEM_ID FROM DUAL UNION
      SELECT 1000 AS JOINT_GROUP_ID, 4 AS ITEM_ID FROM DUAL UNION
      SELECT 1000 AS JOINT_GROUP_ID, 5 AS ITEM_ID FROM DUAL UNION
      SELECT 1000 AS JOINT_GROUP_ID, 7 AS ITEM_ID FROM DUAL UNION
      SELECT 2000 AS JOINT_GROUP_ID, 6 AS ITEM_ID FROM DUAL
    SELECT * FROM T;The relationships are:
    Item 1 is linked to Item 2 by GROUP1 and GROUP2;
    Item 1 is linked to Item 3 by GROUP1 only;
    Item 1 is linked to Item 4 by GROUP2 only;
    Item 1 is linked to Item 5 through Item 4 by GROUP1;
    Item 1 is linked to Item 7 through Item 3 by GROUP2;
    Item 6 is not linked to any other item since it does not match on GROUP1 nor GROUP2 with any other item.
    NOTEs:
    - JOINT_GROUP_ID values could be any sequential value. I used 1000 and 2000 just to avoid confusion with the other IDs and group values used to picture the problem.
    - The level of relationship is not restricted to 2 like the example above. There could be deeper relationships.
    This seems to me like something that could be solved with a hierarchical query, but I could not get my head around it to solve the problem.
    Hope one of you guys can help me on this.
    Chears.

    Hi Bruno,
    You are correct. Frank's solution does not work. You can do this using CONNECT BY on smaller problems, but it will be very inefficient for larger problems wirth significant looping. I wrote a quick blog article on this subject after reading your question this morning. This is actually an example of a very general class of problems and I already had three SQL solutions. I'll put the CONNECT BY one here, and you can look at my blog if you want more details (I include a diagram so I can't just post it here).
    Data
    item_groups
    SQL> SELECT *
      2    FROM item_groups
      3  /
    ITEM_ID    GROUP1                         GROUP2
    01         A                              100
    02         A                              100
    03         A                              101
    04         B                              100
    05         B                              102
    06         C                              103
    07         D                              101
    08         E                              104
    09         E                              105
    10         F                              106
    10 rows selected.Query
    WITH links_v AS (
    SELECT t_fr.item_id node_id_fr,
           t_to.item_id node_id_to,
           t_fr.item_id || '-' || Row_Number() OVER (PARTITION BY t_fr.item_id ORDER BY t_to.item_id) link_id
      FROM item_groups t_fr
      JOIN item_groups t_to
        ON t_to.item_id > t_fr.item_id
       AND (t_to.group1 = t_fr.group1 OR t_to.group2 = t_fr.group2)
    ), nodes_v AS (
    SELECT item_id node_id
       FROM item_groups
    ), tree AS (
    SELECT link_id, CONNECT_BY_ROOT (link_id) root_id
      FROM links_v
    CONNECT BY NOCYCLE (node_id_fr = PRIOR node_id_to OR node_id_to = PRIOR node_id_fr OR
                         node_id_fr = PRIOR node_id_fr OR node_id_to = PRIOR node_id_to)
    ), group_by_link AS (
    SELECT DISTINCT Min (root_id) OVER (PARTITION BY link_id) group_id, link_id
      FROM tree
    ), linked_nodes AS (
    SELECT g.group_id, l.node_id_fr node_id
      FROM group_by_link g
      JOIN links_v l
        ON l.link_id = g.link_id
    UNION
    SELECT g.group_id, l.node_id_to
      FROM group_by_link g
      JOIN links_v l
        ON l.link_id = g.link_id
    SELECT l.group_id "Network", l.node_id "Node"
      FROM linked_nodes l
    UNION ALL
    SELECT '00 (unlinked)', node_id
      FROM nodes_v n
    WHERE n.node_id NOT IN (SELECT node_id FROM linked_nodes)
    ORDER BY 1, 2Output
    Network       Node
    00 (unlinked) 06
                  10
    01-1          01
                  02
                  03
                  04
                  05
                  07
    08-1          08
                  09

Maybe you are looking for

  • Can not use TOUCH function on Nokia 6600 slide

    Can not use TOUCH function on Nokia 6600 slide- turned on sensor in phone settings, but when i touch the PHONE it does not work. Please, tell me how use this function or maybe my phone is out of order

  • Constant HDD access (K8N Neo4 Platinum)

    Hello all, this is my first time posting on these forums. A couple of days ago I put together a new system for myself, with the following components: * Athlon64 3200+ processor * MSI K8N Neo4 Platinum motherboard * 1Gb RAM * Gigabyte 128Mb GeForce 66

  • Auto Fix Red-Eye in Aperture?

    Hello, I'm a newbie to Aperture and I was just trying to figure out where in Aperture can I fix red-eye automatically [similar to how it is done in iPhoto 11]? I realize that I can fix the red eyes manually, but is there a simple button that I can pu

  • Changing document securityGroup on the end of workflow

    Hi, is there any way how to change security group and dDocType after document leaves workflow. The IDOC function wfUpdateMetaData is usable only for 'x' metadata, so we are little stuck here. Thanks for replies, Richard

  • Sharing in FCPX!

    hey guys ok one thing i cant understand!! shoot some stuff in HD, edit it etc and all fine! then when i goto share if i just export it as video, its all good, takes a few secs!! BUT if i choose apple devices>ipad for e.g, it takes ages, in fact i hav