Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

hierachichal sql query

WITH RECURSIVE employee_hierarchy AS (
  SELECT    employee_id,
            first_name,
            last_name,
            reports_to,
            'Owner' AS path
  FROM employee
  WHERE reports_to IS NULL
 
  UNION ALL
   
SELECT
    e.employee_id,
    e.first_name,
    e.last_name,
    e.reports_to,
    employee_hierarchy.path || '->' || e.last_name
  FROM employee e, employee_hierarchy
  WHERE e.reports_to = employee_hierarchy.employee_id
)
SELECT *
FROM employee_hierarchy;
Source by learnsql.com #
 
PREVIOUS NEXT
Tagged: #hierachichal #sql #query
ADD COMMENT
Topic
Name
1+3 =