Course contentsShow
← Data Engineering
Lesson 157 of 8252. Data Modeling for AnalyticsFree lesson

Flattening Hierarchies Using Bridge Tables

Construct bridge tables for hierarchies to enable efficient path-based reporting and rapid aggregation across all levels of a tree structure.

In this lesson, you will learn how to use bridge tables to flatten complex, deep hierarchies into a structure that allows for instant, multi-level reporting.

The Problem with Recursive Hierarchies

Previously, we discussed using Adjacency Lists (where each row points to its parent) to model hierarchies like organizational charts or product categories. While Adjacency Lists are great for storage, they are a nightmare for analytics. Calculating a total for a top-level manager—who oversees departments, teams, and individuals—requires expensive recursive queries or multiple self-joins. These operations slow down dashboards and make it nearly impossible for non-technical users to aggregate data across all descendants.

The Bridge Table Solution

To solve this, we "flatten" the hierarchy into a Bridge Table (also known as an Explosion Table). Instead of storing just the immediate parent, a bridge table stores every ancestor-descendant relationship.

If you have a hierarchy: CEO -> VP -> Manager -> Staff, the bridge table explicitly maps out every path:

  • CEO is an ancestor of VP
  • CEO is an ancestor of Manager
  • CEO is an ancestor of Staff

Implementation Illustration

If your hierarchy table is named Dim_Employees, your bridge table Bridge_Employee_Hierarchy might look like this:

-- Each row represents a relationship between an ancestor and a descendant
SELECT Ancestor_ID, Descendant_ID, Depth_Distance
FROM Bridge_Employee_Hierarchy;

-- Example: CEO (ID: 1) is 2 levels above Manager (ID: 3)
-- Ancestor_ID | Descendant_ID | Depth_Distance
-- 1           | 3             | 2

Why This Works for Analytics

By joining your Fact_Sales table to the Bridge_Employee_Hierarchy table, you can slice by any level of the hierarchy effortlessly. To find total sales for the CEO, you simply filter the bridge table for Ancestor_ID = 1 and join to the facts. The database engine performs a standard join rather than a complex recursive loop, resulting in lightning-fast aggregations across any branch of the tree. This method transforms deep, jagged structures into a flat format that BI tools can consume directly.

Key Takeaway: Bridge tables flatten deep hierarchies by mapping every ancestor-descendant relationship, allowing you to aggregate data across any tree level with simple joins instead of complex recursive queries.