Solved

display id in connect prior sql

Posted on 2014-12-03
3
100 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 24

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File generation using utl_file 4 53
SQL Retrieve Values 4 68
SQL Developer 6 48
pl/sql - query very slow 26 59
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

786 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