Counting Parent Child Level

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cookiejar
    New Member
    • Sep 2008
    • 3

    Counting Parent Child Level

    We are using the following to retrieve parent child:

    [code=oracle]

    Cursor get_child_org is
    SELECT distinct
    o.ORG_ORGANIZAT ION_ID org_id,
    o.name belongs_to,
    uic.code org_uic,
    SYS_CONNECT_BY_ PATH(o.name, ' - ') "ORG_STRUCT URE"
    FROM ORG_ORGANIZATIO NS o,
    org_uic uic
    WHERE o.org_organizat ion_id = uic.org_organiz ation_id(+)
    START WITH o.org_organizat ion_id = vParent_id
    CONNECT BY PRIOR o.org_organizat ion_id = o.owner_organiz ation_id
    Order By ORG_STRUCTURE
    ;
    [/code]

    Is there a method to count the parent child level? I would like to return a count to indicate the level versus the parent child path by appending hyphens '-'.
    Last edited by amitpatel66; Nov 18 '08, 06:52 AM. Reason: code tags
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    And what is the output that you are getting for your query? Yes CONNECT BY PRIOR is the concept of getting hierarchical data.

    Comment

    • Pilgrim333
      New Member
      • Oct 2008
      • 127

      #3
      Hi,

      As you are adding ' - ' between each node, I found you a simple way of getting the level. Each time you add ' - ' this means you are adding another level. So you need a way to count these additions. You could write a function that counts the number of occurances of a character in a string. On the other hand, a much faster way would be adding the following line to your select clause:

      Code:
       length(SYS_CONNECT_BY_PATH(o.name, ' -')) - length(SYS_CONNECT_BY_PATH(o.name, '-')) "level"
      The first sys_connect_by adds 2 chars to the complete path for each child, the second one adds just one char to the complete path for each child. Subtract the length of these two and you have the number of levels. It counts the parent as one, so if you only have the parent, then the result is one. If you don't want to count the parent, then just substract 1 from to result.

      I hope this will help you out, let us know if it does or not. If it doesn't then we'll look for some other solution.

      Pilgrim.

      Comment

      Working...