Solved

display id in connect prior sql

Posted on 2014-12-03
3
105 Views
Last Modified: 2014-12-20
hi how can i diplsay the id of

obj_name

obj_parent

obj_child

and make sure there is no duplicate in obj_name column


WITH hierarchical AS (  

      SELECT  obj_parent

            , li.obj_name  

           , li.obj_type  

           , li.obj_title              , li.DESCRIPTION    

            , LEVELAS lvl

           , SYS_CONNECT_BY_PATH(li.obj_name, '/')paths    

        FROM object_list li  

        LEFTJOIN cal_erd er ON (li.cal_objid = er.obj_child)

           START WITH obj_parent IS NULL  

          CONNECTBY    

                  NOCYCLE PRIOR cal_objid = obj_parent

                   )                        

, childs_parents AS (

              SELECT  obj_name

                    , LEAD(obj_name) OVER(PARTITION BY substr(paths, 1 ,

                                          CASEWHEN INSTR(paths, '/', 1, 2) > 0

                                               THEN INSTR(paths, '/', 1, 2) -1

                                                ELSE LENGTH(paths)

                                           END)ORDERBY paths) AS child_name

                    , substr(paths,                          

                                  INSTR(paths, '/', 1, CASEWHEN lvl > 1 THEN lvl-1 END) + 1 ,    

                                  INSTR(paths, '/', 1, CASEWHEN lvl > 1 THEN lvl END)                      

                                - INSTR(paths, '/', 1, CASEWHEN lvl > 1 THEN lvl-1 END) - 1 ) AS parent_name

                   , obj_type    

                    , obj_title  

                    , DESCRIPTION    

                  

              FROM hierarchical

              )

SELECT *  

  FROM childs_parents;

Open in new window


i what to display value like this
OBJ_CHILD    CHILD_ID    OBJ_ID    OBJ_NAME         OBJ_PARENT    PARENT_ID

ThirdObject      1194         1 193        SecondObject    MainObject    1 192

FourthObject    1195        1 194        ThirdObject        SecondObject   1 193

                          1195                               FourthObject    ThirdObject    1 194

                         1196                               FirthObject      

SecondObject  1193              1 192       MainObject
0
Comment
Question by:chalie001
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40479671
You need to bring these IDs to the hierarchical cte, and then bring them over to the childs_parents cte, like this:
WITH hierarchical AS (  
      SELECT  obj_parent
            , li.obj_name  
           , li.obj_type  
           , li.obj_title              , li.DESCRIPTION    
            , LEVELAS lvl
           , SYS_CONNECT_BY_PATH(li.obj_name, '/')paths
           , li.cal_objid obj_id
        FROM object_list li  
        LEFTJOIN cal_erd er ON (li.cal_objid = er.obj_child)
           START WITH obj_parent IS NULL  
          CONNECTBY    
                  NOCYCLE PRIOR cal_objid = obj_parent
                   )                        
, childs_parents AS (
              SELECT  obj_name
                    , LEAD(obj_name) OVER(PARTITION BY substr(paths, 1 ,
                                          CASE WHEN INSTR(paths, '/', 1, 2) > 0
                                               THEN INSTR(paths, '/', 1, 2) -1
                                                ELSE LENGTH(paths)
                                           END)ORDERBY paths) AS child_name
                    , substr(paths,                          
                                  INSTR(paths, '/', 1, CASEWHEN lvl > 1 THEN lvl-1 END) + 1 ,    
                                  INSTR(paths, '/', 1, CASEWHEN lvl > 1 THEN lvl END)                      
                                - INSTR(paths, '/', 1, CASEWHEN lvl > 1 THEN lvl-1 END) - 1 ) AS parent_name
                   , obj_type    
                    , obj_title  
                    , DESCRIPTION    
                    , obj_id
                    , LEAD(obj_id) OVER(PARTITION BY substr(paths, 1 ,
                                          CASE WHEN INSTR(paths, '/', 1, 2) > 0
                                               THEN INSTR(paths, '/', 1, 2) -1
                                                ELSE LENGTH(paths)
                                           END)ORDERBY paths) AS child_id
                    , obj_parent parent_id
              FROM hierarchical
              )
SELECT *  FROM childs_parents;

Open in new window

0
 

Author Closing Comment

by:chalie001
ID: 40506375
correct
0
 

Author Comment

by:chalie001
ID: 40510894
hi I will like to concaternate connect prior return values to look like this from above query
    Obj_Name       child_name      Parent_name

    MainObject     ThirdObject    (null)

    SecondObject   FourthObject    MainObject

    FourthObject   ThirdObject     SecondObject

    ThirdObject    ThirdObject     SecondObject,MainObject

or
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question