Upload
ecomputernotes
View
222
Download
0
Embed Size (px)
Citation preview
8/3/2019 e computer notes - Hierarchical Retrieval
1/12
Hierarchical Retrieval
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
2/12
Objectives
After completing this lesson, you should be ableto do the following:
" Interpret the concept of a hierarchical query
" Create a tree-structured report
" Format hierarchical data
" Exclude branches from the tree structure
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
3/12
Sample Data from theEMPLOYEESTable
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
4/12
Natural Tree Structure
EMPLOYEE_ID = 100(Parent)
KingANAGER_ID = 100(Child)
Mourgosochhar De Hann HaZlotkey
en Higgins Hunold Rajs Davies Matos Vargas
F
Abel Taylor GrantGietz Ernst Lorentz
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
5/12
Hierarchical Queries
SELECT [LEVEL], column, expr...FROM table
[WHERE condition(s)][START WITHcondition(s)]
[CONNECT BY PRIORcondition(s)] ;
WHEREcondition:
expr comparison_operator expr
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
6/12
Walking the Tree
Starting Point
" Specifies the condition that must be met
" Accepts any valid condition
START WITH
column1 =
value
Using theEMPLOYEEStable, start with the employee
whose last name is Kochhar.
...START WITH last_name = 'Kochhar'
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
7/12
Walking the Tree
CONNECT BY PRIORcolumn1= column2
Walk from the top down, using theEMPLOYEEStable.
... CONNECT BY PRIOR employee_id = manager_id
Direction
Top down Column1 = Parent Key
Column2 = Child Key
Bottom up Column1 = Child KeyColumn2 = Parent Key
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
8/12
Walking the Tree: From the Bottom Up
SELECT employee_id, last_name, job_id, manager_id
FROM employees
START WITH employee_id = 101
CONNECT BY PRIOR manager_id = employee_id ;
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
9/12
Walking the Tree: From the Top Down
SELECT last_name||' reports to '||
PRIOR last_name "Walk Top Down"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id ;
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
10/12
Ranking Rows with theLEVELPseudocolumn
Level 1
root/parent
King Level 2parent/child
Mourgoshar De Hann Hartstein
Zlotkey
Lpar
Higgins Hunold Rajs Davies Matos Vargas
Fay
Abel Taylor Grant LGietz Ernst Lorentz
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
11/12
Formatting Hierarchical Reports UsingLEVELandLPAD
Create a report displaying company managementlevels, beginning with the highest level and indentingeach of the following levels.
LUMN org_chart FORMAT A12LECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_')
AS org_chart
OM employees
ART WITH last_name='King'
NNECT BY PRIOR employee_id=manager_id
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Hierarchical Retrieval
12/12
Pruning Branches
Use theWHEREclause Use theCONNECT BYclaus
to eliminate a node. to eliminate a branch.
ERE last_name != 'Higgins' CONNECT BY PRIORemployee_id = manager_idAND last_name != 'Higgins'
KochharKochhar
Whalen Higgins
Whalen Higgins
Gietz
Gietz
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/