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 7817297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:11:12+00:00 2026-06-02T06:11:12+00:00

I have a single table that contains an ID , Amount , and Parent_ID

  • 0

I have a single table that contains an ID, Amount, and Parent_ID. (It is a little more complex in reality, but this is all the important columns for this example). Basically what i want to do is group and sum the Amount column by the lowest common Parent (ie the ones who’s parent id is NULL).

ID  Amount  Parent_ID
1   100     NULL
2   150     1
3   50      1
4   75      3
5   25      4
6   125     NULL
7   50      6
8   50      7
9   100     8

Expected results:

ID  SUM
1   400
6   325

As you can see, it only retuns two records, the ones who dont have a parent, meaning they are top level items. The Sum column is the sum of all of its children’s Amount recursively, so ID = 1 is the sum of 1,2,3,4 and 5. and ID = 6 is the sum of 6,7,8, and 9.

  • 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-02T06:11:13+00:00Added an answer on June 2, 2026 at 6:11 am

    If you can define a limit to the number of levels in your hierarcy, you can extend this solution to an arbitrary number of levels:

    select
        coalesce(t5.ID,t4.ID,t3.ID,t2.ID,t1.ID) as Root,
        sum(t1.Amount) as Amount
    from Table1 t1
    left join Table1 t2 on t1.Parent_ID = t2.ID
    left join Table1 t3 on t2.Parent_ID = t3.ID
    left join Table1 t4 on t3.Parent_ID = t4.ID
    left join Table1 t5 on t4.Parent_ID = t5.ID
    group by Root
    

    Just add more left join as needed and add them to the list of columns in the coalesce.

    Demo: http://www.sqlfiddle.com/#!2/b7a79/17

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data for a particular stock that contains every single
I have a database that contains a table that looks a bit like this:
I have a single table that contains two types of items under the same
I have this table that contains the ad id and the user id. Any
I'm working on a project that will have a single table holding lots and
Background I have a dimension table that has a single record for each day.
Here's the caveat... If you have a table that has a single column and
I have a feed that is populating a single text field in a table
I have a huge access mdb file which contains a single table with 20-30
I have a table that contains sets of sequential datasets, like that: ID set_ID

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.