Recursive sql query in oracle Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerGet list of all tables in Oracle?Inserting multiple rows in a single SQL query?How do I limit the number of rows returned by an Oracle query after ordering?How can I get column names from a table in SQL Server?SQL Server: How to Join to first rowHow do I UPDATE from a SELECT in SQL Server?What are the options for storing hierarchical data in a relational database?How to export query result to csv in Oracle SQL Developer?

Book with legacy programming code on a space ship that the main character hacks to escape

Would reducing the reference voltage of an ADC have any effect on accuracy?

PIC mathematical operations weird problem

Israeli soda type drink

How to translate "red flag" into Spanish?

Is it acceptable to use working hours to read general interest books?

Align column where each cell has two decimals with siunitx

Co-worker works way more than he should

Need of separate security plugins for both root and subfolder sites Wordpress?

Is Diceware more secure than a long passphrase?

Do I need to protect SFP ports and optics from dust/contaminants? If so, how?

Putting Ant-Man on house arrest

Do you need a weapon for Thunderous Smite, and the other 'Smite' spells?

Will I lose my paid in full property

A Paper Record is What I Hamper

A strange hotel

Why did Israel vote against lifting the American embargo on Cuba?

std::is_constructible on incomplete types

What is "leading note" and what does it mean to "raise a note"?

Why did C use the -> operator instead of reusing the . operator?

Is a 5 watt UHF/VHF handheld considered QRP?

What is it called when you ride around on your front wheel?

What is the ongoing value of the Kanban board to the developers as opposed to management

Arriving in Atlanta after US Preclearance in Dublin. Will I go through TSA security in Atlanta to transfer to a connecting flight?



Recursive sql query in oracle



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerGet list of all tables in Oracle?Inserting multiple rows in a single SQL query?How do I limit the number of rows returned by an Oracle query after ordering?How can I get column names from a table in SQL Server?SQL Server: How to Join to first rowHow do I UPDATE from a SELECT in SQL Server?What are the options for storing hierarchical data in a relational database?How to export query result to csv in Oracle SQL Developer?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Table: ID1 and ID2 are name of the column



| ID1 | ID2 | 
| 4 | 3 |
| 3 | 2 |
| 2 | 1 |
| 7 | 6 |
| 6 | 5 |
| 9 | 8 |


Desired Result



| ID1 | ID2 | 
| 4 | 1 |
| 7 | 5 |
| 9 | 8 |


I need to build a recursive sql query for oracle using connect by or recursive cte. Unable to figure out solution.










share|improve this question
























  • What version of Oracle are you using?

    – Gordon Linoff
    Mar 6 at 12:06











  • current version is 12.1

    – Jai Sethia
    Mar 6 at 12:25

















1















Table: ID1 and ID2 are name of the column



| ID1 | ID2 | 
| 4 | 3 |
| 3 | 2 |
| 2 | 1 |
| 7 | 6 |
| 6 | 5 |
| 9 | 8 |


Desired Result



| ID1 | ID2 | 
| 4 | 1 |
| 7 | 5 |
| 9 | 8 |


I need to build a recursive sql query for oracle using connect by or recursive cte. Unable to figure out solution.










share|improve this question
























  • What version of Oracle are you using?

    – Gordon Linoff
    Mar 6 at 12:06











  • current version is 12.1

    – Jai Sethia
    Mar 6 at 12:25













1












1








1


1






Table: ID1 and ID2 are name of the column



| ID1 | ID2 | 
| 4 | 3 |
| 3 | 2 |
| 2 | 1 |
| 7 | 6 |
| 6 | 5 |
| 9 | 8 |


Desired Result



| ID1 | ID2 | 
| 4 | 1 |
| 7 | 5 |
| 9 | 8 |


I need to build a recursive sql query for oracle using connect by or recursive cte. Unable to figure out solution.










share|improve this question
















Table: ID1 and ID2 are name of the column



| ID1 | ID2 | 
| 4 | 3 |
| 3 | 2 |
| 2 | 1 |
| 7 | 6 |
| 6 | 5 |
| 9 | 8 |


Desired Result



| ID1 | ID2 | 
| 4 | 1 |
| 7 | 5 |
| 9 | 8 |


I need to build a recursive sql query for oracle using connect by or recursive cte. Unable to figure out solution.







sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 12:24







Jai Sethia

















asked Mar 6 at 12:05









Jai SethiaJai Sethia

112




112












  • What version of Oracle are you using?

    – Gordon Linoff
    Mar 6 at 12:06











  • current version is 12.1

    – Jai Sethia
    Mar 6 at 12:25

















  • What version of Oracle are you using?

    – Gordon Linoff
    Mar 6 at 12:06











  • current version is 12.1

    – Jai Sethia
    Mar 6 at 12:25
















What version of Oracle are you using?

– Gordon Linoff
Mar 6 at 12:06





What version of Oracle are you using?

– Gordon Linoff
Mar 6 at 12:06













current version is 12.1

– Jai Sethia
Mar 6 at 12:25





current version is 12.1

– Jai Sethia
Mar 6 at 12:25












2 Answers
2






active

oldest

votes


















2














No need to use CTE in this case since you do not do any cumulative calculations while traversing the tree.



SQL> with t(id1, id2) as
2 (select 4,3 from dual
3 union all select 3,2 from dual
4 union all select 2,1 from dual
5 union all select 7,6 from dual
6 union all select 6,5 from dual
7 union all select 9,8 from dual)
8 select connect_by_root id1 id1, id2
9 from t
10 where connect_by_isleaf = 1
11 start with not exists (select null from t t0 where t0.id2 = t.id1)
12 connect by prior id2 = id1;

ID1 ID2
---------- ----------
4 1
7 5
9 8





share|improve this answer























  • Thanks a ton. It worked for me but I am unable to understand this solution

    – Jai Sethia
    Mar 7 at 9:04


















0














This is just a supplement answer without using Hierarchical queries which removes common elements from id1 and id2.



 WITH t(id1, id2) 
AS (SELECT 4,
3
FROM dual
UNION ALL
SELECT 3,
2
FROM dual
UNION ALL
SELECT 2,
1
FROM dual
UNION ALL
SELECT 7,
6
FROM dual
UNION ALL
SELECT 6,
5
FROM dual
UNION ALL
SELECT 9,
8
FROM dual),
t1
AS (SELECT id1 id1,
id1 id2
FROM t),
t2
AS (SELECT id2 id1,
id2 id2
FROM t),
t3
AS (SELECT ROWNUM row_num1,
id1
FROM (SELECT ( t.id1 ) id1
FROM t
WHERE NOT EXISTS (SELECT NULL
FROM t1,
t2
WHERE ( t1.id1 = t2.id1
AND t1.id2 = t2.id2 )
AND ( t.id1 = t1.id1 ))
ORDER BY t.id1 ASC)),
t4
AS (SELECT ROWNUM row_num1,
id2
FROM (SELECT ( t.id2 ) id2
FROM t
WHERE NOT EXISTS (SELECT NULL
FROM t1,
t2
WHERE ( t1.id1 = t2.id1
AND t1.id2 = t2.id2 )
AND ( t.id2 = t2.id2 ))
ORDER BY t.id2 ASC))
SELECT a.id1,
b.id2
FROM t3 a,
t4 b
WHERE a.row_num1 = b.row_num1
ORDER BY id1;





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55022754%2frecursive-sql-query-in-oracle%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    No need to use CTE in this case since you do not do any cumulative calculations while traversing the tree.



    SQL> with t(id1, id2) as
    2 (select 4,3 from dual
    3 union all select 3,2 from dual
    4 union all select 2,1 from dual
    5 union all select 7,6 from dual
    6 union all select 6,5 from dual
    7 union all select 9,8 from dual)
    8 select connect_by_root id1 id1, id2
    9 from t
    10 where connect_by_isleaf = 1
    11 start with not exists (select null from t t0 where t0.id2 = t.id1)
    12 connect by prior id2 = id1;

    ID1 ID2
    ---------- ----------
    4 1
    7 5
    9 8





    share|improve this answer























    • Thanks a ton. It worked for me but I am unable to understand this solution

      – Jai Sethia
      Mar 7 at 9:04















    2














    No need to use CTE in this case since you do not do any cumulative calculations while traversing the tree.



    SQL> with t(id1, id2) as
    2 (select 4,3 from dual
    3 union all select 3,2 from dual
    4 union all select 2,1 from dual
    5 union all select 7,6 from dual
    6 union all select 6,5 from dual
    7 union all select 9,8 from dual)
    8 select connect_by_root id1 id1, id2
    9 from t
    10 where connect_by_isleaf = 1
    11 start with not exists (select null from t t0 where t0.id2 = t.id1)
    12 connect by prior id2 = id1;

    ID1 ID2
    ---------- ----------
    4 1
    7 5
    9 8





    share|improve this answer























    • Thanks a ton. It worked for me but I am unable to understand this solution

      – Jai Sethia
      Mar 7 at 9:04













    2












    2








    2







    No need to use CTE in this case since you do not do any cumulative calculations while traversing the tree.



    SQL> with t(id1, id2) as
    2 (select 4,3 from dual
    3 union all select 3,2 from dual
    4 union all select 2,1 from dual
    5 union all select 7,6 from dual
    6 union all select 6,5 from dual
    7 union all select 9,8 from dual)
    8 select connect_by_root id1 id1, id2
    9 from t
    10 where connect_by_isleaf = 1
    11 start with not exists (select null from t t0 where t0.id2 = t.id1)
    12 connect by prior id2 = id1;

    ID1 ID2
    ---------- ----------
    4 1
    7 5
    9 8





    share|improve this answer













    No need to use CTE in this case since you do not do any cumulative calculations while traversing the tree.



    SQL> with t(id1, id2) as
    2 (select 4,3 from dual
    3 union all select 3,2 from dual
    4 union all select 2,1 from dual
    5 union all select 7,6 from dual
    6 union all select 6,5 from dual
    7 union all select 9,8 from dual)
    8 select connect_by_root id1 id1, id2
    9 from t
    10 where connect_by_isleaf = 1
    11 start with not exists (select null from t t0 where t0.id2 = t.id1)
    12 connect by prior id2 = id1;

    ID1 ID2
    ---------- ----------
    4 1
    7 5
    9 8






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 6 at 12:43









    Dr Y WitDr Y Wit

    1,470412




    1,470412












    • Thanks a ton. It worked for me but I am unable to understand this solution

      – Jai Sethia
      Mar 7 at 9:04

















    • Thanks a ton. It worked for me but I am unable to understand this solution

      – Jai Sethia
      Mar 7 at 9:04
















    Thanks a ton. It worked for me but I am unable to understand this solution

    – Jai Sethia
    Mar 7 at 9:04





    Thanks a ton. It worked for me but I am unable to understand this solution

    – Jai Sethia
    Mar 7 at 9:04













    0














    This is just a supplement answer without using Hierarchical queries which removes common elements from id1 and id2.



     WITH t(id1, id2) 
    AS (SELECT 4,
    3
    FROM dual
    UNION ALL
    SELECT 3,
    2
    FROM dual
    UNION ALL
    SELECT 2,
    1
    FROM dual
    UNION ALL
    SELECT 7,
    6
    FROM dual
    UNION ALL
    SELECT 6,
    5
    FROM dual
    UNION ALL
    SELECT 9,
    8
    FROM dual),
    t1
    AS (SELECT id1 id1,
    id1 id2
    FROM t),
    t2
    AS (SELECT id2 id1,
    id2 id2
    FROM t),
    t3
    AS (SELECT ROWNUM row_num1,
    id1
    FROM (SELECT ( t.id1 ) id1
    FROM t
    WHERE NOT EXISTS (SELECT NULL
    FROM t1,
    t2
    WHERE ( t1.id1 = t2.id1
    AND t1.id2 = t2.id2 )
    AND ( t.id1 = t1.id1 ))
    ORDER BY t.id1 ASC)),
    t4
    AS (SELECT ROWNUM row_num1,
    id2
    FROM (SELECT ( t.id2 ) id2
    FROM t
    WHERE NOT EXISTS (SELECT NULL
    FROM t1,
    t2
    WHERE ( t1.id1 = t2.id1
    AND t1.id2 = t2.id2 )
    AND ( t.id2 = t2.id2 ))
    ORDER BY t.id2 ASC))
    SELECT a.id1,
    b.id2
    FROM t3 a,
    t4 b
    WHERE a.row_num1 = b.row_num1
    ORDER BY id1;





    share|improve this answer





























      0














      This is just a supplement answer without using Hierarchical queries which removes common elements from id1 and id2.



       WITH t(id1, id2) 
      AS (SELECT 4,
      3
      FROM dual
      UNION ALL
      SELECT 3,
      2
      FROM dual
      UNION ALL
      SELECT 2,
      1
      FROM dual
      UNION ALL
      SELECT 7,
      6
      FROM dual
      UNION ALL
      SELECT 6,
      5
      FROM dual
      UNION ALL
      SELECT 9,
      8
      FROM dual),
      t1
      AS (SELECT id1 id1,
      id1 id2
      FROM t),
      t2
      AS (SELECT id2 id1,
      id2 id2
      FROM t),
      t3
      AS (SELECT ROWNUM row_num1,
      id1
      FROM (SELECT ( t.id1 ) id1
      FROM t
      WHERE NOT EXISTS (SELECT NULL
      FROM t1,
      t2
      WHERE ( t1.id1 = t2.id1
      AND t1.id2 = t2.id2 )
      AND ( t.id1 = t1.id1 ))
      ORDER BY t.id1 ASC)),
      t4
      AS (SELECT ROWNUM row_num1,
      id2
      FROM (SELECT ( t.id2 ) id2
      FROM t
      WHERE NOT EXISTS (SELECT NULL
      FROM t1,
      t2
      WHERE ( t1.id1 = t2.id1
      AND t1.id2 = t2.id2 )
      AND ( t.id2 = t2.id2 ))
      ORDER BY t.id2 ASC))
      SELECT a.id1,
      b.id2
      FROM t3 a,
      t4 b
      WHERE a.row_num1 = b.row_num1
      ORDER BY id1;





      share|improve this answer



























        0












        0








        0







        This is just a supplement answer without using Hierarchical queries which removes common elements from id1 and id2.



         WITH t(id1, id2) 
        AS (SELECT 4,
        3
        FROM dual
        UNION ALL
        SELECT 3,
        2
        FROM dual
        UNION ALL
        SELECT 2,
        1
        FROM dual
        UNION ALL
        SELECT 7,
        6
        FROM dual
        UNION ALL
        SELECT 6,
        5
        FROM dual
        UNION ALL
        SELECT 9,
        8
        FROM dual),
        t1
        AS (SELECT id1 id1,
        id1 id2
        FROM t),
        t2
        AS (SELECT id2 id1,
        id2 id2
        FROM t),
        t3
        AS (SELECT ROWNUM row_num1,
        id1
        FROM (SELECT ( t.id1 ) id1
        FROM t
        WHERE NOT EXISTS (SELECT NULL
        FROM t1,
        t2
        WHERE ( t1.id1 = t2.id1
        AND t1.id2 = t2.id2 )
        AND ( t.id1 = t1.id1 ))
        ORDER BY t.id1 ASC)),
        t4
        AS (SELECT ROWNUM row_num1,
        id2
        FROM (SELECT ( t.id2 ) id2
        FROM t
        WHERE NOT EXISTS (SELECT NULL
        FROM t1,
        t2
        WHERE ( t1.id1 = t2.id1
        AND t1.id2 = t2.id2 )
        AND ( t.id2 = t2.id2 ))
        ORDER BY t.id2 ASC))
        SELECT a.id1,
        b.id2
        FROM t3 a,
        t4 b
        WHERE a.row_num1 = b.row_num1
        ORDER BY id1;





        share|improve this answer















        This is just a supplement answer without using Hierarchical queries which removes common elements from id1 and id2.



         WITH t(id1, id2) 
        AS (SELECT 4,
        3
        FROM dual
        UNION ALL
        SELECT 3,
        2
        FROM dual
        UNION ALL
        SELECT 2,
        1
        FROM dual
        UNION ALL
        SELECT 7,
        6
        FROM dual
        UNION ALL
        SELECT 6,
        5
        FROM dual
        UNION ALL
        SELECT 9,
        8
        FROM dual),
        t1
        AS (SELECT id1 id1,
        id1 id2
        FROM t),
        t2
        AS (SELECT id2 id1,
        id2 id2
        FROM t),
        t3
        AS (SELECT ROWNUM row_num1,
        id1
        FROM (SELECT ( t.id1 ) id1
        FROM t
        WHERE NOT EXISTS (SELECT NULL
        FROM t1,
        t2
        WHERE ( t1.id1 = t2.id1
        AND t1.id2 = t2.id2 )
        AND ( t.id1 = t1.id1 ))
        ORDER BY t.id1 ASC)),
        t4
        AS (SELECT ROWNUM row_num1,
        id2
        FROM (SELECT ( t.id2 ) id2
        FROM t
        WHERE NOT EXISTS (SELECT NULL
        FROM t1,
        t2
        WHERE ( t1.id1 = t2.id1
        AND t1.id2 = t2.id2 )
        AND ( t.id2 = t2.id2 ))
        ORDER BY t.id2 ASC))
        SELECT a.id1,
        b.id2
        FROM t3 a,
        t4 b
        WHERE a.row_num1 = b.row_num1
        ORDER BY id1;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 6:14

























        answered Mar 7 at 9:38









        psaraj12psaraj12

        2,58911526




        2,58911526



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55022754%2frecursive-sql-query-in-oracle%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко