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

  • SEARCH
  • Home
  • 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 42673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:22:30+00:00 2026-05-10T15:22:30+00:00

I have a table similar to this: CREATE TABLE example ( id integer primary

  • 0

I have a table similar to this:

CREATE TABLE example (   id integer primary key,   name char(200),   parentid integer,   value integer); 

I can use the parentid field to arrange data into a tree structure.

Now here’s the bit I can’t work out. Given a parentid, is it possible to write an SQL statement to add up all the value fields under that parentid and recurse down the branch of the tree ?

UPDATE: I’m using posgreSQL so the fancy MS-SQL features are not available to me. In any case, I’d like this to be treated as a generic SQL question.

  • 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. 2026-05-10T15:22:30+00:00Added an answer on May 10, 2026 at 3:22 pm

    There are a few ways to do what you need in PostgreSQL.

    • If you can install modules, look at the tablefunc contrib. It has a connectby() function that handles traversing trees. http://www.postgresql.org/docs/8.3/interactive/tablefunc.html

    • Also check out the ltree contrib, which you could adapt your table to use: http://www.postgresql.org/docs/8.3/interactive/ltree.html

    • Or you can traverse the tree yourself with a PL/PGSQL function.

    Something like this:

    create or replace function example_subtree (integer) returns setof example as 'declare results record;          child record;  begin   select into results * from example where parent_id = $1;   if found then     return next results;     for child in select id from example                   where parent_id = $1       loop         for temp in select * from example_subtree(child.id)         loop           return next temp;         end loop;       end loop;   end if;   return null; end;' language 'plpgsql';  select sum(value) as value_sum   from example_subtree(1234); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table similar to this +--+-----+----+ |ID|Entry|Exit| +--+-----+----+ |18|32154|NULL| +--+-----+----+ |19|NULL |NULL|
I have a data.table object similar to this one library(data.table) c <- data.table(CO =
i have a table which looks similar to this <asp:TableRow><asp:TableCell>Question 1</asp:TableCell><asp:TableCell ID =Question1Text></asp:TableCell></asp:TableRow> <asp:TableRow><asp:TableCell
I have a table with contents that look similar to this: id | title
I have a table that has records with a structure similar to this.. ID
I have a couple of tables with similar structures like this: <table> <tbody> <tr>content</tr>
I have a table similar to the following: id key str_val date_val num_val 1
I have this SQL table: CREATE TABLE DATA ( ID NUMBER NOT NULL, CODE
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
I have a table similar to below in sql server 2005 date_column | field1

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.