Use Of ‘Connect By’ clause for Hierarchal Query

Use Of ‘Connect By’ clause for Hierarchal Query

The Connect By clause is useful for tables which have 2 or more columns with parent-child relationships. While displaying rows, we can use ‘LEVEL’ pseudo column to indent the results as hierarchy.

Consider the following example of a community table:

 Community_IdParent_IdCommunity_Nametmr>Select * from Communities;COMMUNITY_ID  PARENT_ID COMMUNITY_NAME------------ ---------- -------------------------           1          0 TV Entertainment           2          1 NBC           3          1 ABC           4          1 FOX           5          2 Sports           6          2 Movies           7          3 Talk Show           8          3 Documentary           9          7 Educational          10          7 Fun10 rows selected./* Find all children, all leaf lets From Root*/ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities CONNECT BY PRIOR Community_id = Parent_Id START WITH Parent_id= 0Results:COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------TV Entertainment                               1          0   NBC                                         2          1      Sports                                   5          2      Movies                                   6          2   ABC                                         3          1      Talk Show                                7          3         Educational                           9          7         Fun                                  10          7      Documentary                              8          3   FOX                                         4          110 rows selected./* Find immediate children For Parent_Id = 1 : Just go 1 level Down  */ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities CONNECT BY Community_Id = PRIOR  Parent_Id START WITH Parent_Id= 1Results:COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------NBC                                            2          1   TV Entertainment                            1          0ABC                                            3          1   TV Entertainment                            1          0FOX                                            4          1   TV Entertainment                            1          06 rows selected./* Start At Node Community_Id = 7: Traverse up all the way till root */ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities CONNECT BY Community_id = PRIOR  Parent_Id START WITH Community_id = 7Results:COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------Talk Show                                      7          3   ABC                                         3          1      TV Entertainment                         1          0/* Find all children, upto some LEVEL */ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities Where  LEVEL <= 2 CONNECT BY PRIOR Community_Id = Parent_Id START WITH Parent_Id= 0


Results:

 COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------TV Entertainment                               1          0   NBC                                         2          1   ABC                                         3          1   FOX                                         4          1

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing