Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:09:12+00:00 2026-06-14T08:09:12+00:00

This is a simplified version of a problem I am encountering in PostgreSQL. I

  • 0

This is a simplified version of a problem I am encountering in PostgreSQL.

I have the following table A:

[ ID INTEGER | VALUE NUMERIC(10,2) | PARENT INTEGER ]

Where ‘PARENT’ is a self-referencing FK to column ID.

The table definition is:

CREATE TABLE A(ID INTEGER IDENTITY, VALUE NUMERIC(10,2), PARENT INTEGER)                                    
ALTER  TABLE A ADD CONSTRAINT FK FOREIGN KEY (PARENT) REFERENCES A(ID) 

This simple table allows one to define tree data structures of arbitrary depth. Now I need to write a SQL (I prefer not to use server-side PL-SQL) that reports for each node, the total value of the sub-tree “hanging” under it. For instance, with the following table:

|  ID  | VALUE | PARENT |
-------------------------
|   1  | NULL  | NULL   |
|   2  | 3.50  |    1   |
|   3  | NULL  | NULL   |
|   4  | NULL  |    3   |
|   5  | 1.50  |    4   |
|   6  | 2.20  |    4   |

I should get the following result set:

| ID  |  Total-Value-of-Subtree |
|  1  |                  3.50   |
|  2  |                  3.50   |
|  3  |                  3.70   |
|  4  |                  3.70   |
|  5  |                  1.50   |
|  6  |                  2.20   |

For simplicitly, you can assume that only leaf nodes have values, non-leaf nodes always have a value of NULL in the VALUE column. Is there a way to do this in SQL, even utilizing PostgreSQL-specific extensions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:09:13+00:00Added an answer on June 14, 2026 at 8:09 am

    In PostgreSQL you can use recursive CTEs (Common Table Expression) to walk trees in your queries.

    Here are two relevant links into the docs:

    • Syntax
    • Examples

    EDIT

    Since there is no subselect required it might run a little better on a larger dataset than Arion’s query.

    WITH RECURSIVE children AS (
        -- select leaf nodes
        SELECT id, value, parent
            FROM t
            WHERE value IS NOT NULL
        UNION ALL
        -- propagate values of leaf nodes up, adding rows 
        SELECT t.id, children.value, t.parent
            FROM children JOIN t ON children.parent = t.id
    )
    SELECT id, sum(value) 
        FROM children 
        GROUP BY id   -- sum up appropriate rows
        ORDER BY id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the simplified version of the problem: We have a table on an
In this simplified version of my actual problem, I have two tables: User and
Simplified version of the problem: So I have this query which is inside a
This is a simplified version of my problem. I have two buttons, and one
This is a simplified version of an html table I have: (The table is
This is a simplified version of my problem: On the server we have n*m
I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
If I have the following code (simplified version of the problem): class TestA {
This is a simplified version of the original problem. I have a class called
This is a simplified version of the problem I am trying to solve: There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.