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 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

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should probably look into transformations. http://en.csharp-online.net/GDIplus_Graphics_Transformation%E2%80%94Image_Transformation May 11, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer It is possible in Firefox, but it is not standardized,… May 11, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer New programming languages, much like spoken languages, can open up… May 11, 2026 at 6:20 pm

Related Questions

This is very similar to question 653714 , but for MySQL instead of SQL
I have an excel spreadsheet in a format similar to the following... | NAME
If I have a collection of database tables (in an Access file, for example)
I am trying to take a rather large CSV file and insert it into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.