Home » SQL & PL/SQL » SQL & PL/SQL » Unknown number for looping-Logic (10g)
Unknown number for looping-Logic [message #609975] Fri, 14 March 2014 17:12 Go to next message
dev1983
Messages: 2
Registered: March 2014
Junior Member
Hi ,

Please find attached tree node structure.


Source table format is derived from tree structure where Col1 and Col2 contains file names and we have to
find full trace of that filename in below format.

Col1 is 'From filename' and col2 is 'To Filename'

Input Nodes in tabular format:

Source Table format:
COl1,Col2
I,H
G,C
H,F
F,C
J,D
P,K
J,H
K,J
L,J
M,J
M,O
O,E
N,E
D,A
C,A
E,A
E,B
M,Q



Col1 and Col2 are derived from nodes tree where end of chain nodes are A,B,Q

We have to start from end of node/root node.

In above example root nodes are A,B,Q , as they exists in col2 and not in col1

Expected output format:


Col, Root,Branch,Seq
A,1,1,1 /* Root Node with first branch of 'A'*/
E,1,1,2
N,1,1,3 /* End of this branch as there is no col2 for 'N'*/
A,1,2,1 /* Root Node with Second branch of 'A'*/
C,1,2,2
F,1,2,3
H,1,2,4
I,1,2,5 /* End of this branch as there is no col2 for 'I'*/
A,1,3,1 /* Root Node with third branch of 'A'*/
D,1,3,2
J,1,3,3
M,1,3,4 /* End of this branch as there is no col2 for 'M'*/
A,1,4,1 /* Root Node with fourth branch of 'A'*/
C,1,4,2
G,1,4,3 /* End of this branch as there is no col2 for 'G'*/
A,1,5,1 /* Root Node with Fifth branch of 'A'*/
E,1,5,2
O,1,5,3
M,1,5,4 /* End of this branch as there is no col2 for 'M', two branches are finishing at 'M' */
A,1,6,1 /* Root Node with Sixth branch of 'A'*/
D,1,6,2
J,1,6,3
L,1,6,4 /* End of this branch as there is no col2 for 'L'*/
A,1,7,1 /* Root Node with Seventh branch of 'A'*/
D,1,7,2
J,1,7,3
K,1,7,4
P,1,7,5 /* End of this branch as there is no col2 for 'P'*/
A,1,8,1 /* Root Node with Eighth branch of 'A'*/
C,1,8,2
F,1,8,3
H,1,8,4
J,1,8,5
L,1,8,6 /* End of this branch as there is no col2 for 'L',two branches are finishing at 'L' */
A,1,9,1 /* Root Node with Nineth branch of 'A'*/
C,1,9,2
F,1,9,3
H,1,9,4
J,1,9,5
M,1,9,6 /* End of this branch as there is no col2 for 'M', third branche finishing at 'M' */
A,1,10,1 /* Root Node with Tenth branch of 'A'*/
C,1,10,2
F,1,10,3
H,1,10,4
J,1,10,5
K,1,10,6
P,1,10,7 /* End of this branch as there is no col2 for 'P',,two branches are finishing at 'P'*/
B,2,1,1 /* Root Node with First branch of 'B'*/
E,2,1,2
O,2,1,3
M,2,1,4 /* End of this branch as there is no col2 for 'M'*/
B,2,2,1 /* Root Node with Second branch of 'B'*/
E,2,2,2
N,2,2,3 /* End of this branch as there is no col2 for 'N'*/
Q,3,1,1 /* Root Node with First branch of 'Q'*/
M,3,1,2 /* End of this branch as there is no col2 for 'M'*/

Please help with the logic


Thanks,
Dev
  • Attachment: imagetree.jpg
    (Size: 47.50KB, Downloaded 549 times)

[Updated on: Fri, 14 March 2014 18:05]

Report message to a moderator

Re: Unknown number for looping-Logic [message #609981 is a reply to message #609975] Fri, 14 March 2014 23:41 Go to previous messageGo to next message
youngryand
Messages: 10
Registered: March 2014
Location: Madison, WI
Junior Member
Let me first say that it seems clear by your username, and the specific yet vague nature of your question, that you are probably a college student trying to get someone to do their homework for them.

Let me also say that I don't normally spend my weekends this way, but this question really challenged me to beat it, and I became obsessed. I have no doubt that I could solve it with PL/SQL cursors and loops, but I became obsessed with the idea of trying to solve it with a SINGLE SELECT statement that uses hierarchical query operators--an advanced Oracle SQL topic that I've never had a reason to use at any of my jobs. I'm only aware of them through education and certification. It seemed to me that whoever wrote this question was targeting a best solution that uses hierarchical query operators. Therefore I thought I could solve it if I gave it my all.

I was wrong.

I was able to find a partial solution, but it took me over two hours. I'm certain I have the right data, but it's incomplete, and in the wrong format. But I can't spend my whole life on this. I'm sorry this is probably of no use to you.

Note:
The question sample output shows 56 rows, but it's really 13 branches of lengths that total 56.
A length 4 branch takes up four rows.

My output is 13 rows, one branch per row. The whole branch shown in each row.
The number at the end of each row is formatted nicely vertically, but when displayed here in the forum, it's crammed up against the branch letters.

Output:

A,C,F,H,I...........5
A,C,F,H,J,K,P.......7
A,C,F,H,J,L.........6
A,C,F,H,J,M.........6
A,C,G...............3
A,D,J,K,P...........5
A,D,J,L.............4
A,D,J,M.............4
A,E,N...............3
A,E,O,M.............4
B,E,N...............3
B,E,O,M.............4
Q,M.................2


SQL (I put the data into a table called seed_table):


SELECT rpad(to_char(substr(the_path, 2)), 20, '.') || to_char(the_level + 1)
  FROM (
         SELECT sys_connect_by_path(col2, ',') || ',' || col1 the_path, level the_level, connect_by_root col2 root
           FROM seed_table s1
        CONNECT BY PRIOR col1 = col2
       ) branches
 WHERE NOT EXISTS --This ensures we're looking at a branch that goes all the way back to a root
       (
        SELECT 'foo'
          FROM seed_table s2
         WHERE s2.col1 = branches.root
       )
   AND NOT EXISTS --This ensures we're only report branches that go all the way to a leaf
       (
        SELECT *
          FROM (
                 SELECT sys_connect_by_path(col2, ',') || ',' || col1 the_path, level, connect_by_root col2 root
                   FROM seed_table s3
                CONNECT BY PRIOR col1 = col2
               ) branches2
         WHERE NOT EXISTS --This ensures we're looking at a branch that goes all the way back to a root
               (
                SELECT 'foo'
                  FROM seed_table s4
                 WHERE s4.col1 = branches2.root
               )
           AND substr(the_path, 1, length(the_path) - 2) = branches.the_path
       )
 ORDER BY 1;

[Updated on: Fri, 14 March 2014 23:51]

Report message to a moderator

Re: Unknown number for looping-Logic [message #609982 is a reply to message #609975] Fri, 14 March 2014 23:42 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Unknown number for looping-Logic [message #610009 is a reply to message #609981] Sat, 15 March 2014 18:21 Go to previous message
dev1983
Messages: 2
Registered: March 2014
Junior Member
Hi Ryan,

Thanks for the response,

We should get output as 56 rows for 13 branches where
Root - End of chain nodes(root) will be three(1,2,3) in column root(A,B,Q),
Branches(13) - for Root node A(10 Branches), B(2 Branches), Q(1 Branch)
Seq - Sequence number for nodes for each branch starting from root.

I modified your query and got the output like the one you got using below query (added connect_by_isleaf)

select A.the_Path,A.the_Level from
(SELECT sys_connect_by_path(col2, ',') || ',' || col1 the_path,
level the_level,
connect_by_root col2 root,connect_by_isleaf leaf,CONNECT_BY_ISCYCLE as Iscycle
FROM seed_table s1
where connect_by_isleaf=1
CONNECT BY nocycle PRIOR col1 = col2 )A,
(select distinct col2
from seed_table t1
where not exists
(select 1 from seed_table t2 where t1.col2 = t2.col1)) B
where a.root=b.col2

I will try to resolve this using sub tables like datasets instead of using one query. I will post once I get desired output.

Thanks,
Dev

[Updated on: Sat, 15 March 2014 18:36]

Report message to a moderator

Previous Topic: Jpublisher is giving error while generating the class ? ? ?
Next Topic: How to do parallel inserts without changing table parallel degree
Goto Forum:
  


Current Time: Wed Apr 24 18:19:19 CDT 2024