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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:09:58+00:00 2026-05-27T12:09:58+00:00

I know I am probably going about this the wrong way, but I am

  • 0

I know I am probably going about this the wrong way, but I am trying to understand Recursive CTE’s.

I created a simple table

RowNum  Type    Amount
1       Anch    10
2       Amt     1
3       Amt     2
4       Amt     3
5       Amt     4

The idea was to anchor at the amount 10, the to recursively loop through and remove the amount from the total.

I came up with below

WITH cte_Rec (RowNum, [Type], Amount, Amount2, RT, RN)
     AS (SELECT RowNum,
                [Type],
                Amount,
                Amount,
                Amount,
                RowNum
         FROM   dbo.tbl_RecursiveCTE
         WHERE  [Type] = 'Anch'
         UNION ALL
         SELECT r.RowNum,
                r.[Type],
                r.Amount,
                ct.Amount,
                ct.Amount - r.Amount AS RT,
                ct.RowNum
         FROM   dbo.tbl_RecursiveCTE r
                INNER JOIN cte_Rec ct
                  ON ct.RowNum = r.RowNum - 1)
SELECT *
FROM   cte_Rec  

Which obv does not work.

Any ideas?

  • 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-27T12:09:59+00:00Added an answer on May 27, 2026 at 12:09 pm

    Not sure what doesn’t work for you and what exactly you really want …..

    But something like this should work:

    ;WITH cte_Rec AS 
    (
      SELECT RowNum, RowType, Amount AS 'Amount', Amount AS 'SumAmt'
      FROM dbo.tbl_RecursiveCTE
      WHERE RowType = 'Anch'
    
      UNION ALL 
    
      SELECT r.RowNum, r.RowType, r.Amount, CAST(ct.SumAmt - r.Amount AS DECIMAL(18,2))
      from dbo.tbl_RecursiveCTE r
      INNER JOIN cte_Rec ct on ct.RowNum = r.RowNum - 1
    )
    SELECT  *
    FROM cte_Rec
    

    I get an output of:

    RowNum  RowType  Amount SumAmt
     1      Anch     10.00   10.00
     2      Amt       1.00    9.00
     3      Amt       2.00    7.00
     4      Amt       3.00    4.00
     5      Amt       4.00    0.00
    

    The Amount row shows the amount for that specific row, while SumAmt starts with the 10.00 and then consecutively subtracts the other amounts – is that what you’re looking for??

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

Sidebar

Related Questions

I know this probably really simple but Im not sure what im doing wrong...
I may be going about this the wrong way, so I'll set out the
This probably sounds like a stupid question, but I'm going to give it a
As you probably know, Derek Sivers is the guy who created CD Baby and
I know it probably sounds like very trivial question but I couldn't find any
Here's something I know is probably possible but I've never managed to do In
Alright, so as you probably know, static inheritance is impossible in C#. I understand
Okay this is probably a rookie question, but I have never done GUI programming
Most of you probably know the following problem: You edit an HTML, view the
I think I probably know that the most common suggestion will be change the

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.