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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:15:28+00:00 2026-05-27T22:15:28+00:00

I have a self-referencing table with content like this: Self-referencing parent table ID ParentID

  • 0

I have a self-referencing table with content like this:

Self-referencing parent table
ID  ParentID    Name
---------------------
1               John
2   1           Mike
3   2           Erin
4   1           Janie
5               Eric
6   5           Peter

The tree hierarchy should look like this

  • John
    • Mike
      • Erin
    • Janie
  • Eric
    • Peter

And a child table that stores the leaf of parent table that looks like this:

ID  Sales
3   100
3   100
4   200
4   200
6   300
6   300
6   300

I’m trying to roll-up the sum from the leaf node up to the hierarchy so it would return as ..

ID  Name    Sum
1   John    800
2   Mike    200
3   Erin    200
4   Janie   400
5   Eric    900
6   Peter   900

Any ideas how to achieve this in sql 2008? Thanks in advance.

  • 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-05-27T22:15:28+00:00Added an answer on May 27, 2026 at 10:15 pm

    EDIT All aggregation moved out of the CTE

    WITH
      tree AS
    (
      SELECT
        id                AS root_id,
        name              AS root_name,
        id                AS leaf_id
      FROM
        yourTreeTable
    
      UNION ALL
    
      SELECT
        tree.root_id      AS root_id,
        tree.name         AS root_name,
        yourTreeTable.id  AS leaf_id
      FROM
        tree
      INNER JOIN
        yourTreeTable
          ON tree.leaf_id = yourTreeTable.ParentID
    )
    SELECT
      tree.root_id,
      tree.root_name,
      COALESCE(SUM(yourScoresTable.score), 0) AS total
    FROM
      tree
    LEFT JOIN
      yourScoresTable
        ON yourScoresTable.ID = tree.leafID
    GROUP BY
      tree.root_id,
      tree.root_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in my source DB that is self referencing |BusinessID|...|ParentID| This
I have a self-referencing table with an Id, CategoryName, and ParentId. It's a typical
I have a self-referencing many-to-many relationship between users and managers that looks like this:
I have a self referencing table named categories that has a parentcategoryid column that
I have a self referencing table in Oracle 9i, and a view that gets
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have a self-referencing table 'comments' where comments.replyToId REFERENCES comments.ID. My question is, how
I have a table which has a foreign key referencing itself. This would be
I have a standard self referencing table of Categories . In my entity model
Stumped on this one. I have a many to many self referencing relationship(FluentNH) mapped

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.