I need help creating a SQL query that calculates the sum of all child values for each parent node in a hierarchical structure. I’m working with PostgreSQL and need to use a recursive common table expression.
Here’s my table structure:
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
parent_dept INT,
budget DECIMAL(10,2) DEFAULT 0,
is_category INT,
dept_name VARCHAR(200)
);
INSERT INTO departments VALUES
(10, NULL, 'headquarters', 0.0, 1),
(20, 10, 'operations', 0.0, 1),
(30, 10, 'current_ops', 0, 1),
(40, 30, 'sales', 15.0, 0),
(50, 30, 'marketing', 25.0, 0),
(60, 10, 'infrastructure', 0, 1),
(70, 60, 'facilities', 150.0, 0),
(80, 10, 'inventory', 35.0, 0),
(90, 10, 'equipment', 45.0, 0);
I want the query to return each parent node with the total sum of all its descendants. For example, the ‘current_ops’ department should show a total of 40.0 (sales 15.0 + marketing 25.0). The ‘headquarters’ should show the grand total of all departments below it.
How can I write a recursive CTE that traverses the tree structure and calculates these subtotals for each parent node?