hierarchical function

3
Hierarchical Function MOHIT PATEL

Upload: mohit-patel

Post on 01-Oct-2015

214 views

Category:

Documents


1 download

DESCRIPTION

Hierarchical Functions - Oracle SQL

TRANSCRIPT

Hierarchical Function

Hierarchical FunctionMohit PatelSELECTemployee_id,manager_id, LEVEL, sys_connect_by_path(lastname,/) , connect_by_root firstname f1FROMemployeeWHERE employee_id = 4STARTWITHemployee_id=1CONNECTBYNOCYCLE PRIORemployee_id=manager_idAND employee_id != 5ORDER BY LEVEL;123456It will start with the root Node condition. Multiple roots can be there satisfying the conditionParents employee_id will be connected to childs manager_id. PRIOR is used to identify the Parent. NOCYCLE is used to avoid any cycle in the hierarchy.Every time after the link between parent and child is made, AND condition is checked. If it fails then all its subsequent child will not be fetchedAfter all records are selected, records are removed according to the WHERE condition mentionedORDER BY clause is mentioned to ORDER the results** Root is marked as LEVEL 1ORDER SIBLINGS BY employee_idInternal Ordering of Siblingsempidmngridfirstnamelastname10RajPatel21RahulBhatt31MohitPatel42RajeshChou52SureshUber63DeepSaha73ParthSahaEmpidMngridFirstnameLastnameEmppath10RajPatel1/Patel21RahulBhatt1/Patel/Bhatt42RajeshChou1/Patel/Bhatt/Chou31SureshUber1/Patel/Bhatt/Uber52MohitPatel1/Patel/Patel63DeepSaha1/Patel/Patel/Saha73ParthSaha1/Patel/Patel/Saha31MohitPatel3/Patel63DeepSaha3/Patel/Saha73ParthSaha3/Patel/Sahaselect employee_id, manager_id, firstname, last_name , connect_by_root employee_id emp , sys_connect_by_path(last_name,'/') pathFROM emp START WITH last_name='Patel' CONNECT BY NOCYCLE prior employee_id=manager_id