Build recursive Common Table Expression queries for hierarchical data
Build a recursive CTE for this hierarchical data requirement:
**Table Structure:**
```sql
{{TABLE_STRUCTURE}}
```
**Hierarchy Type:** {{HIERARCHY_TYPE}}
(e.g., org chart, category tree, BOM, graph traversal)
**Query Requirement:** {{REQUIREMENT}}
**Max Depth:** {{MAX_DEPTH}}
**Database System:** {{DATABASE_SYSTEM}}
Please provide:
1. Recursive CTE query with:
- Anchor member
- Recursive member
- Termination condition
2. Step-by-step execution explanation
3. Path tracking implementation
4. Cycle detection if needed
5. Performance optimization tips
6. Depth limiting implementation
7. Alternative approaches (nested sets, materialized path)
8. Sample data and expected outputOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{TABLE_STRUCTURE][{HIERARCHY_TYPE][{REQUIREMENT][{MAX_DEPTH][{DATABASE_SYSTEM]