Vous êtes sur la page 1sur 11

Getting Started with Oracle 12c

Hierarchical Retrieval

Description:
BISP is committed to provide BEST learning material to the beginners and advance learners. In the same series, we have prepared a complete end-to end Hands-on Beginners G ide !or "racle #nal$tic % nctions. The doc ment !oc ses Gro ping and #ggregating data sing S&'. (oin o r pro!essional training program and learn !rom e)perts.

History:
Version Description Change 0.1 Initial Draft 0.1 $e%iew&1 Author Pawan Ma anan A'it (har'a Publish Date 10th !ct "01# 10th !ct "01#

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 1

Contents:
Contents:............................................................................................................................................................. 2 Hierarchical Queries:........................................................................................................................................... 3 2) Applying ORDER S !" #$S !% clause:.........................................................................................................& 3) Applying the S%S'CO##EC( '!%')A(H :...................................................................................................* +) Applying CO##EC('!%' S "EA, :............................................................................................................... .) ,or/atting Hierarchical Reports 0y using "E1E" an2 ")AD :......................................................................13

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 2

Hierarchical Queries:
A0out Hierarchical Queries: Retrie4e 2ata using hierarchical 5ueries 6 0ase2 on hierarchical relationship 0et7een ro7s in a ta0le. Oracle 0eing a relational 2ata0ase6 2oes not store recor2s in hierarchical 7ay 6 0ut i8 any parent9chil2 relationship e:ists 0et7een ro7s 6 7e can construct a hierarchy 0et7een the ro7s. ;ith help o8 a process calle2 (ree ;al<ing 6 7e can create hierarchy 0et7een ro7s in single ta0le. A hierarchical 5uery is a /etho2 o8 reporting6 in or2er6 the 0ranches o8 a tree.. Synta: o8 a Hierarchical 5uery: SE"EC( ="E1E">6 colu/n6 e:pr... ,RO? ta0le =;HERE con2ition@s)> =S(AR( ; (H con2ition@s)> =CO##EC( !% )R OR con2ition@s)> A ;ith the help o8 BSTART WITHC 6you speci8y the starting point 6 ie root ro7 (or rows) o8 the tree. ;ith the help o8 BCONNECT BYC6you speci8y the 2irection in 7hich hierarchy is 7al<e2 6you speci8y the con2itions o8 parent9chil2 relationship in a hierarchy. RIOR re8ers the !are"t ro7 (o e:ecute Hierarchical 5ueries 6 a/ going to use ta0le Ta#les use$ : salessta%% Salessta%% ta0le 0asically contains 2ata a0out the people in the sales 2eparte/ent. !e8ore using Hierarchical 5ueries6 lets see 7hole o8 the 2ata present in salessta%% ta0le. ,or 7e 7ill 0e e:ecuting the 8ollo7ing 5uery : Select & %ro' salessta%%( (hen clic< on BrunC 0utton speci8ie2 in 0elo7 i/age to e:ecute the 5uery .

Out!ut:

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 3

"et s say 7e 7ant to <no7 the 2etails : Re)uire'e"t: Display /anage/ent reporting line ie 2isplay the /angers o8 2i88erent 0ranches an2 the 0ranch e/ployees. Quer*:

Sa'e )uer*:

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page +

"ine la0elle2 16 has Bstart 7ithC clause 7hich 2e8ines the root o8 the hierarchical structure. "ine la0elle2 26 2e8ines the parent9chil2 relationship an2 2e8ines the 2irection in 7hich hierarchy is 7al<e2 . So 6 7hiche4er ro7s satis%ies the co""ect #* co"$itio"s are retrie4e2 as part o8 the hierarchy. Out!ut :

n the a0o4e i/age6 you can see the /anage/ent reporting line 6 ie !ranch ?anagers o8 2i88erent 0ranches along 7ith the 0ranch e/ployees 2etails. !loc< la0ele2 as B1C6 are the 2etails o8 the 0ranch e/ployees along 7ith their !ranch ?anager6 8or the !ranch 7ith co2e B&C. !loc< la0ele2 as B2C6 are the 2etails o8 the 0ranch e/ployees along 7ith their !ranch ?anager6 8or the 0ranch co2e BDC. !loc< la0ele2 as B3C6 are the 2etails o8 the 0ranch e/ployees along 7ith their !ranch ?anager6 8or the 0ranch 7ith co2e B-C. !loc< la0ele2 as B+C6 are the 2etails o8 the 0ranch e/ployees along 7ith their !ranch ?anager6 8or the 0ranch 7ith co2e B13C. !loc< la0ele2 as B.C6 are the 2etails o8 the 0ranch e/ployees along 7ith their !ranch ?anager6 8or the 0ranch 7ith co2e B1+C.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page .

E+!la"atio": nternally 8irst 6the ta0le is ,oi"e$ 7ith itsel86 then 7e use the -start with. clause to 2eter/ine the starti"/ !oi"t ie 7hich are the root ro7s o8 the hierarchy 6 then 7e apply -co""ect #*. con2itions@parent9chil2 relationship) to ro7s 67hich 2eci2es ro7s that are going to 0e the part o8 the hierarchy . Note: -0evel. colu/n in the a0o4e ta0le is a seu$ocolu'"6 7hich represents the le4el o8 the tree. All the /anagers o8 2i88erent 0ranches are at le4el B1C an2 rest e/ployees in le4el B2C. Note: DonEt use $ROF) !% or ORDER !% 7ith hierarchical 5ueries6 other7ise it 7oul2 o4erri2e the hierarchical or2er.

1) A!!l*i"/ OR2ER SIB0IN3S BY clause:


Fse2 to or2er chil2 ro7s o8 the sa/e parent . t 8irst preser4es the or2er speci8lie2 in the hierarchical 5uery clause an2 then applies the or2er si0lings clause to the si0lings or@chil2ren) Re)uire'e"t: Details o8 the 0ranch /anager ha4ing last na/e as B;rightC along 7ith e/ployees o8 the correspon2ing 0ranch. ie Display the /anage/ent le4els 0eginning 7ith 0ranch /anager@ ha4ing lastna/e as B;rightC) an2 0elo7 le4els. Quer* :

n the a0o4e 5uery 6 7e ha4e 2e8ine2 the root o8 the tree as person ha4ing lastna/e as B;rightC.@it happens that this person is actually a 0ranch /anager 6 7e <no7 it 0e8orehan2) An2 connect 0y clause pro4i2es the e/ployees present in that 0ranch. Out!ut: @BE4ORE 5SIN3 -OR2ER SIB0IN3S BY.):

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page &

"ine la0ele2 as 1 6 is the root o8 the hierarchy6as they ha4e le4el 1 an2 "ines la0ele2 as 26 are the chil2ren o8 the parent root ro7 as they ha4e le4el 2 Notice that the chil2ren o8 the root ro7 are not in any or2er 6 i8 7e 7ant to sort the/ 7e can use OR2ER SIB0IN3S BY NOW 5SIN3 -OR2ER SIB0IN3S BY. 6o$i%ie$ )uer*:

New out!ut :

Si0lings are sorte2 in 2escen2ing or2er ASC 90atically.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page D

7) A!!l*i"/ the SYS8CONNECT 8BY8 ATH :


Fse2 in Hierarchical 5ueries t returns the path o8 a colu/n 4alue @speci8ie2 in the 8irst argu/ent) 8ro/ root to no2e 67ith colu/n 4alues separate2 0y char @speci8ie2 in the secon2 argu/ent)
Example Syntax: SYS_CONNECT_BY_PATH (column , char)

Quer*:

Out!ut :

ath 2isplays the path 8ro/ root to other no2es.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page *

9) A!!l*i"/ CONNECT8BY8IS 0EA4 :


s a )seu2ocolu/n . Returns 1 i8 the current ro7 is a lea8 other7ise 3. 5SE: can 0e use2 to chec< i8 a particular ro7 can 0e e:pan2e2 8urther or not . Quer*:

Out!ut:

n the a0o4e output 6 e:cept the root 6the rest ro7s @as they are the lea8s in hierarchy) ha4e 4alue 1 in the Bis8a8lea%C colu/n.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page -

:) 4or'atti"/ Hierarchical Re!orts #* usi"/ 0E;E0 a"$ 0 A2 :


Displaying a hierarchical report 2isplaying the /anage/ent le4els starting 7ith the highest le4el an2 the su0se5uent le4els in2ente2. Quer*:

Out!ut :

n the a0o4e output6 colu/n Blastna/eB has 0een in2ente2 accor2ing to the le4el o8 that particular ro7 . E+!la"atio": (o 2o proper in2entation6 7e use2 ")AD 8unction an2 "E1E" pseu2ocolu/n . www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 13

")AD@G G6@le4el91)H2) : it le8t pa2s the I E @8irst argu/ent) 7ith nu/0er o8 spaces until the pa22ing has length e5ual to the nu/0er returne2 0y the secon2 argu/ent. 1) returns an e/pty string 8or le4elJ1 @ie 8or root 6no in2ention is applie2 8or the root ro7) 2) returns a string consisting o8 2 7hitespaces6 8or le4elJ2@ie 8or chil2 ro7s 6 in2entation is applie2 to the le8t si2e accor2ing to le4el6 here 2 space in2entation is applie2 to the le8t si2e)

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 11

Vous aimerez peut-être aussi