Relationship parent child

Hello

My data are like that.

PARENT CHILD
1
6
7
1 12
1 14
1 15
6 61
6 65
7 71
12 120
12 124
12 127
71 712
712-7122

My required output is
Parent child
1 12
1 120 (12 is the parent for this. But I need to Parent of 12 or 1 (Parent) main)
1 124 - do-
1 127 - do-
7 71
7 712 (71 is the parent for this. But I need Parent 71 or 7 (main Parent))
7-7122 (712's parent.)

The origin of this child, I need. How to get there. PL. help.

Thanks in advance

Mano.

Hello

This one?

SQL>
SQL> with data as (
  2  select null parent , 1 child from dual union all
  3  select null , 6 from dual union all
  4  select null , 7 from dual union all
  5  select 1, 12 from dual union all
  6  select 1, 14 from dual union all
  7  select 1, 15 from dual union all
  8  select 6, 61 from dual union all
  9  select 6, 65 from dual union all
 10  select 7, 71 from dual union all
 11  select 12, 120 from dual union all
 12  select 12, 124 from dual union all
 13  select 12, 127 from dual union all
 14  select 71, 712 from dual union all
 15  select 712, 7122 from dual )
 16  select connect_by_root(child),child  from data
 17  where level > 1
 18  start with parent is null
 19  connect by prior child = parent
 20  /

CONNECT_BY_ROOT(CHILD)      CHILD
---------------------- ----------
                     1         12
                     1        120
                     1        124
                     1        127
                     1         14
                     1         15
                     6         61
                     6         65
                     7         71
                     7        712
                     7       7122

11 rows selected.

SQL> 

T

The amended version looks more like your needs

Published by: ttt on 17.3.2010 01:16

Tags: Database

Similar Questions

  • Add an object 3d without parent/child relationship.

    Hello

    I have a scene of 3d picture control, I have two objects in there, but when I created the scene I had to use the add object invoke node objects in the scene, so there is a parent/child relationship between the objects, which means that if I move the parent, I also move the child object. Is there a way to create a scene with several objects without the parent/child relationship.

    Thank you.

    -Luis

    Hello, I solved it by first moving the objects and then adding them to the scene. -Luis

  • parent/child relationship hirarchy

    Hi everyone, consider the sample data

    SELECT LFROM '1TR1Y6', NULL LTO, MID '11ZFDD', '21XHK7' double UNION ALL TID-

    SELECT '1TR1YF' LFROM, LTO NULL, '11ZFDD' MI '21XHK5' TID FROM dual UNION ALL-

    SELECT '1TR1X1' LFROM, LTO NULL, '11ZFDD' MI '21XHK8' TID FROM dual UNION ALL-

    SELECT '1LSZX1' LFROM, LTO NULL, '11ZFDD' MI '1TR1YD' TID FROM dual UNION ALL-

    SELECT LFROM "1LSZWQ", "21XHK7" LTO, MID '11ZFDD', '1TR1Y6' TID FROM dual UNION ALL-

    SELECT LFROM "1LSZX2", "21XHK5" LTO, MID '11ZFDD', '1TR1YF' TID FROM dual UNION ALL-

    SELECT LFROM "1LSZVG", "21XHK8" LTO, MID '11ZFDD', '1TR1X1' TID FROM dual UNION ALL-

    SELECT LFROM "4LSSVG", "21XHK9" LTO "11AAA' MI '1TR1X1' TID double UNION ALL

    SELECT "AAAAA" LFROM, LTO NULL, '11ABB' e, 'CCCCCC' double UNION ALL TID

    SELECT "BATH" LFROM, 'CCCCCC' LTO, MID '11ABB', 'AAAAA' double UNION ALL TID

    some of them have a parent/child releationship. for example, let take rank with LFROM = 1TR1Y6

    1TR1Y6 (lfrom) corresponds to the fifth line with TID 1TR1Y6.  TID 1TR1Y6 LTO COLUMN value corresponds to the first line TID (21XHK7)

    It before these two LINES are IN a parent/child relationship.

    lets take another example.  LINE two WITH LFROM = 1TR1YF matches sixth RANK WITH TID 1TR1YF.  the sixth LINE LTO COLUMN (21XHK5) corresponds to the SECOND TID of LINE.

    Once again the parent child relationship. There are two lines that do not have the relationship of parent/child (ex.  line 8)

    Im trying to write a query that can partition the data in the middle column and find the parent/child relationship. the output of the above data should be

    LTO LFROM MIDDLE TID FLAG

    1TR1Y6 NULL 11ZFDD 21XHK7 1

    1LSZWQ 21XHK7 11ZFDD 1TR1Y6 1

    1TR1YF NULL 11ZFDD 21XHK5 1

    1LSZX2 21XHK5 11ZFDD 1TR1YF 1

    1TR1X1 NULL 11ZFDD 21XHK8 1

    1LSZVG 21XHK8 11ZFDD 1TR1X1 1

    1LSZX1 NULL 11ZFDD 1TR1YD 0

    4LSSVG 21XHK9 11AAA 1TR1X1 0

    ABDELKÉBIR 11ABB CCCCCC NULL 1

    CCCCCC 11ABB AAAAA BATH 1

    For simplicity, I posted the release with parent/child, side by side in the sequence.  the order does not matter as long as the flag column (pull the column) is set to an appropriate value (1 if parent/child, 0 no parent/child)

    the flag column value is derived from column to indicate the parent/child relationship was found.

    I try using lead, the lag function but I have problem of sorting data from parent and child line could come at random order (they are not side by side).

    It is therefore difficult to use lead because you don't know how many lines to carry forward.

    can someone help me write a query that displays the output above

    SELECT T1. LFROM,

    T1. LTO,

    T1. ENVIRONMENT,

    T1. TID,

    (

    SELECT COUNT (*)

    OF TBL T2

    WHERE)

    T2. TID = T1. LFROM

    OR

    T2. LFROM = T1. TID

    )

    AND T2. MI = T1. MID

    ) FLAG

    OF TBL T1

    /

    LTO LFROM MIDDLE TID FLAG
    ------ ------ ------ ------ ----------
    1LSZWQ 21XHK7 11ZFDD 1TR1Y6 1
    1LSZX2 21XHK5 11ZFDD 1TR1YF 1
    1LSZVG 21XHK8 11ZFDD 1TR1X1 1
    CCCCCC 11ABB AAAAA BATH 1
    1TR1X1 11ZFDD 21XHK8 1
    1TR1X1 11ZFDD 21XHK8 1
    1LSZX1 11ZFDD 1TR1YD 0
    1TR1Y6 11ZFDD 21XHK7 1
    ABDELKÉBIR 11ABB CCCCCC 1
    1TR1YF 11ZFDD 21XHK5 1
    4LSSVG 21XHK9 11AAA 1TR1X1 0

    11 selected lines.

    SQL >

    SY.

  • in the query and parent/child relationship

    WITH the data AS

    (

    SELECT '213NY1' lfrom, NULL, '215ZVD' mi lto, "215ZV9 ' id, 1 January 2014' sdate, January 2, 2014' edate, 3 January, 2014 'mdate' January 5, 2014 medate double UNION ALL.

    Lfrom SELECT NULL, '215ZVD' mi, id "213NY1", "215ZV9" lto 4 January 2014 'sdate', 6 January 2014 edate, January 7, 2014 'mdate', 8 January 2014 medate double UNION ALL

    SELECT '216TVZ' lfrom, NULL, "213JW7" e "217LVQ" id lto, 21 January, 2014 'sdate' January 9, 2014 edate, 10 January 2014 'mdate', 11 January 2014 medate double UNION ALL

    SELECT lfrom "215Y71", "217LVQ" lto, mi, "216TVZ" id "213JW7", 22 January, 2014 'sdate' January 23, 2014 edate, 24 January, 2014 'mdate' January 25, 2014 medate double UNION ALL

    SELECT '234IJF' lfrom, NULL, "234YU" e "3IUED" id lto, 26 January 2014 'sdate', 27 January 2014 edate, 28 January 2014 'mdate', 29 January 2014 medate OF double

    )

    some lines have parent/child relationship and some does not. for example, the first two query (data) are link together. online, the value of lto (215ZV9) corresponds to the id value

    in line 1.  the value of lfrom in row1 (213NY1) corresponds to the value of online id.

    the same scenario occurs in rows 3 and 4.  5th doesn't have any line which is a child. tier 5 is a single parent

    I want to WRITE a query that gives the following result.

    Mid id initial_date final_date

    =======================================

    215ZVD 215ZV9 1 January 2014 "January 5, 2014"

    "215ZVD 213NY1 7 January 2014" January 6, 2014 "

    "213JW7 217LVQ 21 January 2014 ' 11 January 2014"

    "213JW7 216TVZ 24 January 2014 ' 23 January 2014"

    "234YU 3IUED 26 January 2014 ' 27 January 2014"

    ONLY WHEN there is a parent/child relationship, initial DATE AND follow the final data WHAT such as

    FIRST date of deadline DATE

    < Mdate > < EDate > - for child

    < SDate > < MEDate > - for parent

    WHEN there is no relationship of parent/child (e.g. ONLY a parent ROW) THEN

    FIRST date of deadline DATE

    < SDate > < EDate-> parent

    can someone help me write a query for the above output

    with tt AS

    (SELECT a.*,

    ROW_NUMBER () ON LV (MIDDLE ORDER BY SDATE PARTITION),

    CASE

    WHEN ID = NVL (ADVANCE (lto, 1) OVER (PARTITION BY Middle ORDER BY sdate), LAG (lfrom) OVER (ORDER BY sdate Middle PARTITION))

    THEN 1

    END flg

    DATA one

    )

    SELECT THE MIDDLE,

    ID,

    CASE

    WHEN flg IS NOT NULL

    THEN

    CASE

    WHAT LV = 1

    THEN SDATE

    WHAT LV = 2

    THEN mDATE

    END

    Of ANOTHER sdate

    END INITIALDATE

    CASE

    WHEN flg IS NOT NULL

    THEN

    CASE

    WHAT LV = 1

    THEN mEDATE

    WHAT LV = 2

    THEN EDATE

    END

    Of ANOTHER edate

    END FINALDATE

    TT;

    Output:

    -------------

    MID ID INITIALDATE FINALDATE

    213JW7 217LVQ 21 January 2014 January 11, 2014

    213JW7 216TVZ 24 January 2014 23 January 2014

    215ZVD 215ZV9 January 1, 2014 5 January 2014

    215ZVD 213NY1 January 7, 2014 6 January 2014

    234YU 3IUED 26 January 2014 January 27, 2014

  • Relationship of child to parent SQL-

    Hi all

    I'm writing a sql - and the requirement is as below.

    Data in the table contains columns Parent | Child | Summary_Flag

    If the child's Parent again to some other child then summary flag is "O".

    e.g. parent | Child | Summary_flag

    P1 C1 N
    P2 P3 Y
    P3 C2 N

    This relationship exists at the level of four-. that is level to what child can be parent again.

    I'm writing SQL that will give me the data in the columns - i.e. Parent1 | Parent2 | Parent3 | Parent4 | Child.

    Pleas help in every way possible.

    Thank you
    Bobin

    Hello

    What is your desired output?

    Like this.

    P1 | C1
    P2 | P3 | C2

    SELECT   REGEXP_REPLACE (MAX (res)KEEP (DENSE_RANK LAST ORDER BY lvl),
                             '/'
                            ) res
        FROM (SELECT     PARENT,
                         SYS_CONNECT_BY_PATH (   CASE
                                                    WHEN LEVEL = 1
                                                       THEN PARENT
                                                 END
                                              || '|'
                                              || CHILD,
                                              '/'
                                             ) res,
                         LEVEL lvl
                    FROM test_data
                   WHERE  summary_flag = 'N'
              CONNECT BY  PRIOR CHILD = PARENT)
    GROUP BY PARENT
    
    SQL> WITH test_data AS
      2       (SELECT 'P1' PARENT, 'C1' CHILD, 'N' summary_flag
      3          FROM DUAL
      4        UNION ALL
      5        SELECT 'P2' PARENT, 'P3' CHILD, 'Y' summary_flag
      6          FROM DUAL
      7        UNION ALL
      8        SELECT 'P3' PARENT, 'P4' CHILD, 'Y' summary_flag
      9          FROM DUAL
     10        UNION ALL
     11        SELECT 'P4' PARENT, 'C2' CHILD, 'N' summary_flag
     12          FROM DUAL)
     13  SELECT   REGEXP_REPLACE (MAX (res)KEEP (DENSE_RANK LAST ORDER BY lvl),
     14                           '/'
     15                          ) res
     16      FROM (SELECT     PARENT,
     17                       SYS_CONNECT_BY_PATH (   CASE
     18                                                  WHEN LEVEL = 1
     19                                                     THEN PARENT
     20                                               END
     21                                            || '|'
     22                                            || CHILD,
     23                                            '/'
     24                                           ) res,
     25                       LEVEL lvl
     26                  FROM test_data
     27                 WHERE  summary_flag = 'N'
     28            CONNECT BY  PRIOR CHILD = PARENT)
     29  GROUP BY PARENT;
    
    RES
    ---------------------------------------------------------------------------------------------
    P1|C1
    P2|P3|P4|C2
    
    SQL> 
    

    Salim cordially.

  • Simulate the 1 - N N - N relationship using the Parent-child structure

    Hello

    I have a 1-N relation between SKILL tables and the USER, which means 1 skill can be applied to multiple users. A jurisdiction may have a parent jurisdiction, which means that a ParentSkill inherits all the privileges of his child.

    I would like to simulate a N - N relation between the users and the skills by building a vision that goes through the parent-child relationship. This is possible for the construction?


    Current structure:
    |USERID | USERNAME | SKILLID  |
    +-------+----------+----------+
    |   1   |  Jack    |      1   |
    |   2   |  Simon   |      1   |
    |   3   |  Fred    |      3   |
    +-------+----------+----------+
    
    
    |SKILLID | DESCRIPTION   | PARENTSKILL  |
    +--------+---------------+--------------+
    |   1    |  Mechanic     |      2       |
    |   2    |  Inspector    |      3       |
    |   3    |  Supervisor   |      null    |
    +--------+---------------+--------------+
    Favorite exit (the skillid order is not important):
    |USERID | USERNAME | SKILLID  |
    +-------+----------+----------+
    |   1   |  Jack    |      1   |
    |   2   |  Simon   |      1   |
    |   3   |  Fred    |      3   |
    |   3   |  Fred    |      2   |
    |   3   |  Fred    |      1   |
    +-------+----------+----------+

    I hope that answers your needs:

    WITH
    users as
    (
        SELECT 1 as USERID, 'Jack' as USERNAME, 1 as SKILLID FROM DUAL UNION ALL
        SELECT 2 as USERID, 'Simon' as USERNAME, 1 as SKILLID FROM DUAL UNION ALL
        SELECT 3 as USERID, 'Fred' as USERNAME, 3 as SKILLID FROM DUAL
    ),
    skills as
    (
        SELECT SKILLID,CONNECT_BY_ROOT SKILLID AS ANCESTORS
        FROM
            (
                SELECT 1 as SKILLID, 'Mechanic' as DESCRIPTION, 2 as PARENTSKILL FROM DUAL UNION ALL
                SELECT 2, 'Inspector', 3 FROM DUAL UNION ALL
                SELECT 3, 'Supervisor', NULL FROM DUAL
            )
        CONNECT BY PRIOR PARENTSKILL = SKILLID
    )
    SELECT USERID,USERNAME,ANCESTORS
    FROM skills, users
    WHERE users.skillid = skills.skillid
    /
    
        USERID USERN  ANCESTORS
    ---------- ----- ----------
             1 Jack           1
             2 Simon          1
             3 Fred           3
             3 Fred           2
             3 Fred           1
    

    I hope this helps!

  • Parent - child relationship constraint Unique 5.0 apex oracle

    Oky so I will try to avoid duplicate values get inserted in the parent-child node.

    So basically when there's a node parent (Mobile) I can't create same node with the same name.

    Even for the child node, if there is a child node name there may be another named A.

    Structure:

    Mobile (parent) /Android (Child) /KitKat (subchild) (allowed)

    Mobile/apple/ios(Now allowed Same Parent name)

    Win7/Windows/win7(not allowed same child)

    So a big child and the parent must be unique at any time.

    77.JPG

    create an index of single "TEST_UK" on tablename (parent_id, upper (child_name));

    see this too for a case-insensitive unique constraint

    Case-insensitive unique constraint

  • Default filters that users can delete (in the parent/child Setup)?

    Is it possible in Latitude 2.2.2 to have filters (filters of the range, refinement filters etc.) applied by default (which means that users see these filters when they log-in), but they can also remove (using bread crumbs portlet) if they wanted to? We tried to use the source of data (JSON) filters (using 'baseFunctions' setting) where we have a parent/child Setup, but it seems these filters cannot be removed once applied. Note that filters can be removed if we do not have a parent/child relationship between the data sources, but as soon as we introduce a parent/child relationship between the data sources (which we need), the GET of sticky filters and cannot be deleted. Any reason why the parent/child relationship causes the filters becoming sticky? Any way (alternate) to reach what we want?

    I think that it is a known problem (LSTUDIO-5088) and there could be a fix for this. You can contact technical support to obtain the fix.

    Dave

  • Fusion of data/result defines what are parent/child?

    Hello:

    I have seen a few examples to make a data model so that a BI report can have master-detail data. They are a set of Q1 data and a set of T2 data with a variable binding to connect the two in a parent/child relationship.

    But what I need, it is actually a UNION. I have the same table on multiple instances of database and I want to show the results grouped in a single report, as if it all happened to a table. Examples of master-detail have a section defining the grouping but they group Q1 (see here)

    http://blogs.Oracle.com/BIDeveloper/data_template/

    and I need to regroup in a column in Q1 and Q2 to "merge" the two sets of data.

    Does anyone do this?

    Thank you.

    If you define multiple SQL-queries/data models data sets, you can combine them into a single set of data by selecting the concatenated SQL Data Source.

    Choose concatenate them box, concatenation will result in the union.

    RTF again, you can use the two dataset and display as a single table.

  • Parent child query

    I got the exit for parent-child relationships

    Select
    LPAD (' ', 2 * level) | child
    Of
    prune_test
    Start by
    parent is null
    connect
    child precondition = parent

    as
    1
    -12
    -120
    -124
    -127
    -712
    -7122
    -7125
    -7127

    But I want like
    Parent child Child1 2 child3
    1
    1 12
    1 12 120
    1-12-124
    1-12-127
    1 12 712
    1 12 712 7122
    1 12 712 7125
    1 12 712 7127

    Can someone help me...
    Thank you
    Mani

    Published by: Mani on November 4, 2010 18:54

    Do you just need a sys link by road

    with prune_test as
    (select null parent, 1 child from dual union all
     select 1, 12 from dual union all
     select 12, 120 from dual union all
      select 12, 124 from dual union all
       select 12, 127 from dual union all
       select 12, 712 from dual union all
       select 712,71522 from dual union all
       select 712, 7125 from dual union all
       select 712, 7127 from dual
       )
    
    select
    lpad(' ', 2*level) || child, sys_connect_by_path(child,' ')
    from
    prune_test
    start with
    parent is null
    connect by
    prior child=parent 
    
    LPAD('',2*LEVEL)||CHILD     SYS_CONNECT_BY_PATH(CHILD,'')
      1           1
        12            1 12
          120      1 12 120
          124      1 12 124
          127      1 12 127
          712      1 12 712
            71522      1 12 712 71522
            7125      1 12 712 7125
            7127      1 12 712 7127
    
  • Stuck on a sql query to search for records that have the same parent child records

    Oracle 10 g 2 Enterprise Edition.

    Hello

    I'm writing a logic to find records in a parent table, who have the same values in a child table.
    This is part of a larger application, but I am stuck on that part for now, so I have mocked some of the below simplified tables to capture the heart of the
    the problem is that I'm stuck.
    Let's say I have a responsible parent, child employee table table and there are a number of many relationships between them.
    The aptly named Join_Table manages the relationship between them. If a manager can manage several employees, an employee can be managed by
    many managers.

    I have a feeling it's stupidly easy, but it seems to me having a bad episode of brain freeze today!
    -- parent table
    CREATE TABLE manager (
     id      number primary key,
     name      varchar2(100));
    
    -- child table 
    CREATE TABLE employee (
     id          number primary key,
     name      varchar2(100));
    
    -- link table
    CREATE TABLE join_table (
     manager_id          NUMBER, 
     employee_id      NUMBER,
     CONSTRAINT join_table_pk PRIMARY KEY (manager_id, employee_id),
     CONSTRAINT manager_fk FOREIGN KEY (manager_id) REFERENCES manager(id),
     CONSTRAINT employee_fk FOREIGN KEY (employee_id) REFERENCES employee(id) 
     );
    
    -- Insert some managers
    INSERT INTO manager (id, name) VALUES (1, 'John');
    INSERT INTO manager (id, name) VALUES (2, 'Bob');
    INSERT INTO manager (id, name) VALUES (3, 'Mary');
    INSERT INTO manager (id, name) VALUES (4, 'Sue');
    INSERT INTO manager (id, name) VALUES (5, 'Alan');
    INSERT INTO manager (id, name) VALUES (6, 'Mike');
    
    -- Insert some employees 
    INSERT INTO employee (id, name) VALUES (101, 'Paul');
    INSERT INTO employee (id, name) VALUES (102, 'Simon');
    INSERT INTO employee (id, name) VALUES (103, 'Ken');
    INSERT INTO employee (id, name) VALUES (104, 'Kevin');
    INSERT INTO employee (id, name) VALUES (105, 'Jack');
    INSERT INTO employee (id, name) VALUES (106, 'Jennifer');
    INSERT INTO employee (id, name) VALUES (107, 'Tim');
    
    -- Insert the links
    -- John manages Paul, Simon, Ken
    INSERT INTO join_table (manager_id, employee_id) VALUES (1, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (1, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (1, 103);
    -- Bob manages Paul, Simon, Kevin, Jack
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 104);
    INSERT INTO join_table (manager_id, employee_id) VALUES (2, 105);
    -- Mary manages Jennifer, Tim
    INSERT INTO join_table (manager_id, employee_id) VALUES (3, 106);
    INSERT INTO join_table (manager_id, employee_id) VALUES (3, 107);
    -- Sue manages Jennifer, Tim
    INSERT INTO join_table (manager_id, employee_id) VALUES (4, 106);
    INSERT INTO join_table (manager_id, employee_id) VALUES (4, 107);
    -- Alan manages Paul, Simon, Ken, Jennifer, Tim
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 103);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 106);
    INSERT INTO join_table (manager_id, employee_id) VALUES (5, 107);
    -- Mike manages Paul, Simon, Ken
    INSERT INTO join_table (manager_id, employee_id) VALUES (6, 101);
    INSERT INTO join_table (manager_id, employee_id) VALUES (6, 102);
    INSERT INTO join_table (manager_id, employee_id) VALUES (6, 103);
    
    -- For sanity
    CREATE UNIQUE INDEX employee_name_uidx ON employee(name);
    If I ask for Manager John, so I want to find other managers who manage the exact list and even employees.
    Answer should be Mike.
    If I ask for Manager of Mary, the answer should be Sue.

    This query will give me the list of managers who manage some of the same employees as John, but not the same employees accurate...
    SELECT DISTINCT m.name AS manager
    FROM manager m, join_table jt, employee e
    WHERE m.id = jt.manager_id
    AND jt.employee_id = e.id
    AND e.id IN (
         SELECT e.id
         FROM manager m, join_table jt, employee e
         WHERE m.id = jt.manager_id
         AND jt.employee_id = e.id
         AND m.name = 'John')
    ORDER BY 1;
    I thought about using set operations to find managers with a list of employees less than my employees is null and where my employees under their list of employees is null. But there must be an easier way more elegant.
    Any ideas?
    BTW, I need to run as a batch on tables with > 20 million rows so the efficiency of queries is key.

    What about...

    WITH manager_list AS
    (
     SELECT name,
            LTRIM(MAX(SYS_CONNECT_BY_PATH(id,','))
            KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
     FROM   (SELECT m.name,
                    e.id,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev
             FROM   manager m,
                    join_table jt,
                    employee e
      WHERE m.id           = jt.manager_id
      AND   jt.employee_id = e.id
      AND   m.name = :P_MANAGER)
      GROUP BY name
      CONNECT BY prev = PRIOR curr AND name = PRIOR name
      START WITH curr = 1
    ), all_list AS
    (
     SELECT name,
            LTRIM(MAX(SYS_CONNECT_BY_PATH(id,','))
            KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
     FROM   (SELECT m.name,
                    e.id,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr,
                    ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev
             FROM   manager m,
                    join_table jt,
                    employee e
      WHERE m.id           = jt.manager_id
      AND   jt.employee_id = e.id)
      GROUP BY name
      CONNECT BY prev = PRIOR curr AND name = PRIOR name
      START WITH curr = 1
    )
    SELECT a.*
    FROM   manager_list m,
           all_list a
    WHERE  m.employees = a.employees
    

    Would be easier in 11g, but I do not have a facility here so this is based on 10g.

    See you soon

    Ben

  • Update the data to uppercase in the parent/child tables

    Hi gurus!

    In production, we have a table product and that is in reference by many tables making parent child relationship. By mistake, we realized last month some products have been added to lowercase and now we have a request to update all these product codes uppercase so that existing code that use these tables have no impact. Appreciate if you can give an idea about how can I update the existing data in the table parent uppercase as well as child records?

    Concerning
    Sri

    Are the product code that you need to update what is stored in the tables of children? If Yes, then you must do it in several steps, something like:

    Identify the child tables

    SELECT table_name, constraint_name
    FROM user_constraints
    WHERE r_constraint_name = (SELECT constraint_name
                               FROM user_constraints
                               WHERE table_name = 'PRODUCT_TABLE' and
                                     constraint_type = 'P');
    

    Create new product of upper-case code in the product table:

    INSERT INTO product_table
    SELECT UPPER(product_code), other_columns
    FROM product_table
    WHERE product_code <> UPPER(product_code);
    

    Update the children tables for caps product codes

    UPDATE child1
    SET product_code = UPPER(product_code)
    WHERE product_code <> UPPER(product_code);
    

    Finally, remove the tiny product_table product codes

    DELETE FROM product_table
    WHERE product_code <> UPPER(product_code);
    

    John

  • Sum of a stand-alone parent-child table

    I have two tables - a 'key' table containing a parent-child relationship of multi-layer and a table 'amount' that contains the keys for the nodes in the key as well as table of numeric values (for example the amounts).
    I want a query that returns each line in the table of key as well as the sum of the amount amounts of the table for the set of nodes of this key (so the root node would be the sum of all values of quantity).

    Here's what I mean: I have two tables, the KEY and the AMOUNT

    KEY has two columns, keys, and parent_key; key and parent_key have a relationship CONNECT BY parent_key = prior key (with null for the root parent_key):
    KEY     PARENT_KEY
    0       null
    1       0
    2       0
    3       0
    1A      1
    2A      2
    2B      2
    3A      3
    3B      3
    3C      3
    1A1     1A
    1A2     1A
    2A1     2A
    2A2     2A
    2B1     2B
    3A1     3A
    3A2     3A
    3C1     3C
    3C2     3C
    AMOUNT a two columns, keys, and amount; key points to KEY.key and amount is a value for that particular key
    (Note that all key values are leaf nodes in the KEY table)
    KEY     AMOUNT
    1A1     1 
    1A2     2 
    2A1     3 
    2A2     4 
    2B1     5 
    3A1     6 
    3A2     7 
    3C1     8 
    3C2     9 
    What I want is a result that looks like this, where the amount of each key is the sum of the amounts of its possible keys sheet
    KEY     AMOUNT
    0       45
    1        3
    2       12
    3       30
    1A       3
    2A       7
    2B       5
    3A      13
    3B       0
    3C      17
    1A1      1
    1A2      2
    2A1      3
    2A2      4
    2B1      5
    3A1      6
    3A2      7
    3C1      8
    3C2      9
    For example, the value of the key 2 a, 7, is the sum of the values of 2 a 1 and a 2, 2; key value of 3 is the sum of 3 a 1, a 3, 2, 3, C 1 and 2 of 3.

    Is it possible to do this with a single query?
    The idea I came with is, do a select on the KEY with a "Key CONNECT_BY_PATH" column so that each line contains a string that contains the keys of all his ancestors and then do a join on AMOUNT with IN the CONNECT_BY_PATH amount.key column; However, with a large amount of data, it takes a little time. Is there a way faster / more obvious to achieve?

    OK you have almost everything you need. Outer join just your two tables, and then perform a hierarchical query, noting the key root of each tree. Then group the data set resulting:

    with t1 as (select '0' "KEY", '' parent_key from dual
      union all select '1', '0' from dual
      union all select '2', '0' from dual
      union all select '3', '0' from dual
      union all select '1A', '1' from dual
      union all select '2A', '2' from dual
      union all select '2B', '2' from dual
      union all select '3A', '3' from dual
      union all select '3B', '3' from dual
      union all select '3C', '3' from dual
      union all select '1A1', '1A' from dual
      union all select '1A2', '1A' from dual
      union all select '2A1', '2A' from dual
      union all select '2A2', '2A' from dual
      union all select '2B1', '2B' from dual
      union all select '3A1', '3A' from dual
      union all select '3A2', '3A' from dual
      union all select '3C1', '3C' from dual
      union all select '3C2', '3C' from dual
    ), t2 as (select '1A1' "KEY", 1 amount from dual
      union all select '1A2', 2 from dual
      union all select '2A1', 3 from dual
      union all select '2A2', 4 from dual
      union all select '2B1', 5 from dual
      union all select '3A1', 6 from dual
      union all select '3A2', 7 from dual
      union all select '3C1', 8 from dual
      union all select '3C2', 9 from dual
    ), t3 as (
      select t1.key, parent_key, amount
        from t1 left join t2 on t1.key = t2.key
    ), t4 as (
      select CONNECT_BY_ROOT key root_key
           , level lv
           , t3.*
        from t3
        connect by prior t3.key = parent_key
    )
    select root_key "KEY", sum(amount) amount
      from t4
     group by root_key
     order by length(root_key), root_key;
    
  • Blockages with parent/child tables

    Guys,

    We have a parent-child of tables in our database with the DDL below:

    CREATE TABLE 'ACCOUNT '.

    (SELECT 'ACCOUNT_ID' VARCHAR2 (32 BYTE) NOT NULL,)

    X, Y, Z,...),

    'XPK_ACCOUNT' CONSTRAINT PRIMARY KEY ('ACCOUNT_ID');

    CREATE TABLE ACCOUNT_PARTITION ".

    (SELECT 'ACCOUNT_ID' VARCHAR2 (32 BYTE) ENABLE NOT NULL, "BALANCE" NUMBER (5.0) NOT NULL,)

    X, Y, Z,...),

    'XPK_ACCOUNT_PARTITION' CONSTRAINT PRIMARY KEY ('ID_COMPTE', 'BALANCE')

    KEY FOREIGN CONSTRAINT 'ACCOUNT_PARTITION_FK1' ('ACCOUNT_ID')

    SELECT THE 'ACCOUNT' ('ACCOUNT_ID') REFERENCES);

    "Lately, we cross " ""ORA-00060: Deadlock detected while you wait for resources ' errors while DELETING in the tables above. Please note that the tables of the child is not any clue except KP index.


    Could someone please suggest resolution for us to minimize the error of blocking?


    Respect,

    Bugs

    Imagine you remove account, Oracle must verify there is no record of the child in ACCOUNT_PARTITION, this is because as your FK is restrict the deletions (do not allow the removal if there are child records).  As ACCOUNT_PARTITION. Account_id is indexed so it will be an exclusive table lock to find this, locks the table.

    Another way of thinking is, imagine that you have performed the following query without and ACCOUNT_ID index:

    Select *.

    of ACCOUNT_PARTITION

    where ACCOUNT_ID =

    He SHOULD do a full table scan right?  Well well, even on a deletion of the parent. It attempts to identify lines for the purpose of blocking,

    As a general rule, it is always a good idea to index FKs.

  • Oracle query convert recursive parent-child level columns

    Dear gurus,

    I have a Hyperion extracted dimension table with my COA in a parent/child layout:

    parent, child

    Acc01, acc001

    acc001, acc0001 etc.

    I need to convert to the:

    level 2, level 1, Level0

    Acc01, acc001, acc0001 etc.

    Please advise, need to feed table table schema for reporting.

    Concerning

    Yannis

    with

    data in the form of

    (select "C01" parent, child 'C001' Union double all the)

    Select 'C001', 'C0001' from dual union all

    Select 'C001', 'C0002"dual union all

    Select "C01", "C002' from dual union all

    Select "C0001', 'C00001' from dual union all

    Select "C00001', 'C000001' from dual union all

    Select 'C000001', 'C0000001' from dual

    )

    Select leve1_1, level_2, level_3, level_4, level_5, level_6

    from (select root0, lvl0, regexp_substr(path,'[^,]+',1,level) VAC, level lvl

    of (lvl0 level select,

    connect_by_root parent root0,

    parent connect_by_root | path of SYS_CONNECT_BY_PATH (Child, ',')

    from the data

    where connect_by_isleaf = 1

    Start with parent (select parent

    data d

    If not exists (select null

    from the data

    where child = d.parent

    )

    )

    connect by prior child = parent

    )

    connect by level<=>

    and prior root0 = root0

    and prior lvl0 = lvl0

    and prior sys_guid() is not null

    )

    Pivot (max (VAC) for lvl 1 as leve1_1, 2 level_2, 3 level_3, 4 level_4, 5 level_5, 6 level_6)

    LEVE1_1 LEVEL_2 LEVEL_3 LEVEL_4 LEVEL_5 LEVEL_6
    C01 C002 - - - -
    C01 C001 C0002 - - -
    C01 C001 C0001 C00001 C000001 C0000001

    Concerning

    Etbin

Maybe you are looking for