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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:59:00+00:00 2026-06-07T09:59:00+00:00

i need to do a recursive sum in SQL Server. I want a stored

  • 0

i need to do a recursive sum in SQL Server. I want a stored procedure where i can pass in a parent Id, then return a total for all the children(and children’s children) linked to that parent id.

Here is what i have so far

IF object_id('tempdb..#Averages') IS NOT NULL
BEGIN
   DROP TABLE #Averages
END


CREATE TABLE #Averages
(
ID INT PRIMARY KEY CLUSTERED IDENTITY(1,1),
Name VARCHAR(255),
ParentID int,
Value INT
)

INSERT INTO #Averages(Name,ParentID,Value)VALUES('Fred',NULL,1)
INSERT INTO #Averages(Name,ParentID,Value)VALUES('Bets',NULL,1)

INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Wynand',ID,21 FROM #Averages WHERE      Name = 'Fred'  )

INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Dewald',ID,27 FROM #Averages WHERE     Name = 'Fred'  )
INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Katelynn',ID,1 FROM #Averages WHERE Name = 'Dewald'  )

INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Jacques',ID,28 FROM #Averages WHERE Name = 'Bets'  )
INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Luan',ID,4 FROM #Averages WHERE Name = 'Jacques'  )
INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Ruben',ID,2 FROM #Averages WHERE Name = 'Jacques'  )


;WITH Personal AS
(
SELECT N=1, ID,Name,ParentID,Value
FROM #Averages 
WHERE ParentID IS NULL
UNION ALL
SELECT N+1, Av.ID,Av.Name,Av.ParentID,Av.Value
FROM #Averages Av
INNER JOIN  Personal P ON P.ID = Av.ParentID
)

SELECT Name,
    SUM(Value) as Total
FROM Personal
WHERE N<=3
GROUP BY Name
  • 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-07T09:59:02+00:00Added an answer on June 7, 2026 at 9:59 am

    After playing around a bit i think i got it. I added a Top Level ID, which i set in the Root for the CTE. And then just add the top level id for all the recursion.

    In the end i only sum, and basically use TopLevelId to join to the Top level Table.

    IF object_id('tempdb..#Averages') IS NOT NULL
    BEGIN
       DROP TABLE #Averages
    END
    
    CREATE TABLE #Averages
    (
        ID INT PRIMARY KEY CLUSTERED IDENTITY(1,1),
        Name VARCHAR(255),
        ParentID int,
        Value INT
    )
    
     INSERT INTO #Averages(Name,ParentID,Value)VALUES('Fred',NULL,1)
     INSERT INTO #Averages(Name,ParentID,Value)VALUES('Bets',NULL,1)
    
     INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Wynand',ID,21 FROM #Averages WHERE Name = 'Fred'  )
    
     INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Dewald',ID,27 FROM #Averages WHERE Name = 'Fred'  )
     INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Katelynn',ID,1 FROM #Averages WHERE Name = 'Dewald'  )
    
     INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Jacques',ID,28 FROM #Averages WHERE Name = 'Bets'  )
     INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Luan',ID,4 FROM #Averages WHERE Name = 'Jacques'  )
     INSERT INTO #Averages(Name,ParentID,Value)(SELECT 'Ruben',ID,2 FROM #Averages WHERE Name = 'Jacques'  )
    
    
    ;WITH Personal AS
    (
        SELECT N=1, 
            ID,
            Name,
            ParentID,
            Value,
            TopLevelID =ID
        FROM #Averages 
        WHERE ParentID IS NULL
    
        UNION ALL
    
        SELECT N+1, 
            Av.ID,
            Av.Name,
            Av.ParentID,
            Av.Value,
            TopLevelID =P.TopLevelID
        FROM #Averages Av
        INNER JOIN  Personal P ON P.ID = Av.ParentID
    )
    
    SELECT SUM(P.Value) AS Total,
            A.Name
    FROM Personal P
    INNER JOIN #Averages A on A.ID = P.TopLevelID
    GROUP BY A.Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help building a loop in my stored procedure, basically i want it
I need to write a non-recursive version of the function sum-squares and Use a
I need to write a recursive function that can add two numbers (x, y),
I need a recursive function in C , that checks/compare the sum of values(integers)
I need a recursive function that will find all controls on a page and
I need some help on the SimpleXML calls for a recursive function that lists
I have some sort of recursive function, but I need to parse a string,
I need to recursively go through all properties in my class and if the
I need to copy two linked lists recursively and return a new list .
A table exists in Microsoft SQL Server with record ID, Start Date, End Date

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.