les19

14
19 Copyright © Oracle Corporation, 2001. All rights reserved. Hierarchical Retrieval

Upload: abdul-ghaffar

Post on 23-Dec-2015

213 views

Category:

Documents


0 download

DESCRIPTION

dssd

TRANSCRIPT

Page 1: les19

19Copyright © Oracle Corporation, 2001. All rights reserved.

Hierarchical Retrieval

Page 2: les19

19-2 Copyright © Oracle Corporation, 2001. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:

• Interpret the concept of a hierarchical query

• Create a tree-structured report

• Format hierarchical data

• Exclude branches from the tree structure

Page 3: les19

19-3 Copyright © Oracle Corporation, 2001. All rights reserved.

Sample Data from the EMPLOYEES Table

Page 4: les19

19-4 Copyright © Oracle Corporation, 2001. All rights reserved.

Natural Tree Structure

De HannDe Hann

KingKing

HunoldHunold

EMPLOYEE_ID = 100 (Parent)

MANAGER_ID = 100 (Child)

WhalenWhalen

KochharKochhar

HigginsHiggins

MourgosMourgosZlotkeyZlotkey

RajsRajs DaviesDavies MatosMatos

GietzGietz ErnstErnst LorentzLorentz

HartsteinHartstein

Fay Fay

AbelAbel TaylorTaylor GrantGrant

VargasVargas

Page 5: les19

19-5 Copyright © Oracle Corporation, 2001. All rights reserved.

Hierarchical Queries

WHEREWHERE conditioncondition::WHEREWHERE conditioncondition::

expr comparison_operator expr

SELECT [LEVEL], column, expr...FROM table[WHERE condition(s)][START WITH condition(s)][CONNECT BY PRIOR condition(s)] ;

Page 6: les19

19-6 Copyright © Oracle Corporation, 2001. All rights reserved.

Walking the Tree

Starting Point

• Specifies the condition that must be met

• Accepts any valid condition

Using the EMPLOYEES table, start with the employee

whose last name is Kochhar.

...START WITH last_name = 'Kochhar'

START WITH column1 = value

Page 7: les19

19-7 Copyright © Oracle Corporation, 2001. All rights reserved.

Walking the Tree

Direction

Top down Top down Column1 = Parent KeyColumn1 = Parent KeyColumn2 = Child KeyColumn2 = Child Key

Bottom up Bottom up Column1 = Child KeyColumn1 = Child KeyColumn2 = Parent KeyColumn2 = Parent Key

Walk from the top down, using the Walk from the top down, using the EMPLOYEESEMPLOYEES table.table.Walk from the top down, using the Walk from the top down, using the EMPLOYEESEMPLOYEES table.table.

CONNECT BY PRIOR column1 = column2

... CONNECT BY PRIOR employee_id = manager_id

Page 8: les19

19-8 Copyright © Oracle Corporation, 2001. All rights reserved.

Walking the Tree: From the Bottom Up

SELECT employee_id, last_name, job_id, manager_idFROM employeesSTART WITH employee_id = 101CONNECT BY PRIOR manager_id = employee_id ;

Page 9: les19

19-9 Copyright © Oracle Corporation, 2001. All rights reserved.

Walking the Tree: From the Top Down

SELECT last_name||' reports to '|| PRIOR last_name "Walk Top Down"FROM employeesSTART WITH last_name = 'King'CONNECT BY PRIOR employee_id = manager_id ;

Page 10: les19

19-10 Copyright © Oracle Corporation, 2001. All rights reserved.

Ranking Rows with the LEVEL Pseudocolumn

De HannDe Hann

KingKing

HunoldHunoldWhalenWhalen

KochharKochhar

HigginsHiggins

MourgosMourgosZlotkeyZlotkey

RajsRajs DaviesDavies MatosMatos

GietzGietz ErnstErnst LorentzLorentz

HartsteinHartstein

Fay Fay

AbelAbel TaylorTaylor GrantGrant

VargasVargas

Level 1root/parent

Level 2parent/child

Level 3parent/child

/leaf

Level 4leaf

Page 11: les19

19-11 Copyright © Oracle Corporation, 2001. All rights reserved.

Formatting Hierarchical Reports Using LEVEL and LPAD

Create a report displaying company management levels, beginning with the highest level and indenting each of the following levels.

COLUMN org_chart FORMAT A12SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS org_chartFROM employees START WITH last_name='King' CONNECT BY PRIOR employee_id=manager_id

Page 12: les19

19-13 Copyright © Oracle Corporation, 2001. All rights reserved.

Pruning Branches

Use the Use the WHEREWHERE clause clause to eliminate a node.to eliminate a node.

Use the Use the CONNECT BYCONNECT BY clause clauseto eliminate a branch.to eliminate a branch.

WHERE last_name != 'Higgins'WHERE last_name != 'Higgins' CONNECT BY PRIOR CONNECT BY PRIOR employee_id = manager_id employee_id = manager_id AND last_name != 'Higgins'AND last_name != 'Higgins'KochharKochhar

HigginsHiggins

GietzGietz

WhalenWhalen

KochharKochhar

HigginsHigginsWhalenWhalen

GietzGietz

Page 13: les19

19-14 Copyright © Oracle Corporation, 2001. All rights reserved.

Summary

In this lesson, you should have learned the following:

• You can use hierarchical queries to view a hierarchical relationship between rows in a table.

• You specify the direction and starting point ofthe query.

• You can eliminate nodes or branches by pruning.

Page 14: les19

19-15 Copyright © Oracle Corporation, 2001. All rights reserved.

Practice 19 Overview

This practice covers the following topics:

• Distinguishing hierarchical queries from nonhierarchical queries

• Walking through a tree

• Producing an indented report by using the LEVEL pseudocolumn

• Pruning the tree structure

• Sorting the output