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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:01:16+00:00 2026-05-27T21:01:16+00:00

I have a result-set below Rank cardID DespatchValue Spend SumSpend Spendtype 0 468612 500

  • 0

I have a result-set below

Rank    cardID      DespatchValue   Spend   SumSpend    Spendtype
0       468612      500                0          0         Despatch
1       468612      500             -8500       -8500       Topup
2       468612      500            -11500       -20000      Topup
3       468612      500             -3500       -23500      Topup

As you can see each spend is added to the Sum spend (via a recursive CTE) which i am happy with.

However the logic needs to work thus

  • Despatch value up to initial spend = Spend type Balance
  • Anything after that = Top up

So,

I would like to see :

Rank    cardID  DespatchValue   Spend   SumSpend    Spendtype
0       468612  500               0        0        Despatch
?       468612  500             -500    -500        Balance
1       468612  500             -8000   -8500       Topup
2       468612  500             -11500  -20000      Topup
3       468612  500             -3500   -23500      Topup

Any ideas on how to achieve this?

  • 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-27T21:01:16+00:00Added an answer on May 27, 2026 at 9:01 pm

    To do a split row, you’ll need to add two more UNION clauses to your CTE. Most recursive CTE’s have an “anchor” and a recursive bit. You’ll need to expand how that recursive bit works. First, you identify when to surpress the “nominal” case, which for you is when the balance transitions from a positive total to negative. In your case that’s “DespatchValue + SumSpend”.

    The next two terms will be your “split” rows. Using the inverse of whatever condition is above (ie, only bring back the rows over this “special” condition). The first UNION will be be the “BALANCE” record, taking it down to 0. The second is the remainder of what’s left over.

    Here’s what I came up with – note that I added a “subid” column to indicate the “balance” (and to prevent duplicates in the later-on recursion). I also added a “DespatchBalance”, assuming that’s really what you’re checking against. Specifics aside, the general format should be good:

    declare @txn as table (
        id int, 
        cardID int, 
        DespatchValue int, 
        Spend int
        --SumSpend int, 
        --Spendtype varchar(50)
    )
    
    insert into @txn
                        select 0, 468612, 500, 0--, 0, 'Despatch'
    union all select 1, 468612, 500, -8500--, -8500, 'Despatch'
    union all select 2, 468612, 500, -11500--, -20000, 'Despatch'
    union all select 3, 468612, 500, -3500--, -23500, 'Despatch'
    
    
    ;with x (id, subid, cardID, DespatchValue, Spend, SumSpend, DespatchBalance, Despatch) as 
    (
        --Anchor - beginning record
        select id, 0, cardID, DespatchValue, Spend
        , Spend as SumSpend
        , DespatchValue as DespatchBalance
        , 'Despatch' as Despatch
        from @txn
        where id = 0
    
        UNION ALL       
        -- primary CTE - identify all nominal 'Topup' records
        select t.id, 0, t.cardID, t.DespatchValue, t.Spend
        , x.SumSpend + t.Spend
        , x.DespatchBalance + t.Spend
        , 'Topup' 
        from @txn t
        join x
            on x.id + 1 = t.id
            and x.subid = 0
        where x.DespatchBalance <= 0
    
        UNION ALL 
    
        -- These two UNIONs do a split record:
    
        -- First half of split - the remaining amount to a balance of 0
        select t.id, 1 -- special "subid" to indicate it's a split record
        , t.cardID, t.DespatchValue
        , - x.DespatchBalance
        , x.SumSpend - x.DespatchValue -- only capture the remaing bit above balance
        , 0 -- DespatchBalance should be 0
        , 'Balanace' 
        from @txn t
        join x
            on x.id + 1 = t.id
            and x.subid = 0
        where x.DespatchBalance > 0
            and x.DespatchBalance + t.Spend < 0
    
        UNION ALL 
        -- Second half of split - record that this is an overflow after "Balance" reached
        select t.id, 0
        , t.cardID, t.DespatchValue
        , t.Spend + x.DespatchBalance
        , x.SumSpend + t.Spend 
        , x.DespatchBalance + t.Spend
        , 'Topup' 
        from @txn t
        join x
            on x.id + 1 = t.id
            and x.subid = 0
        where x.DespatchBalance > 0
            and x.DespatchBalance + t.Spend < 0
    )
    
    select *
    from x
    option (MAXRECURSION 100)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that returns a result set similar to the one below:
Have this result set below; am trying to insert the red numbers into post_position
I have a query that returns a result set similar to the one below
I have a result set in MS-SQL within a stored procedure, and lets say
I have a large result set assembled in a parent/child relationship. I need to
I have a SOAP result set that the nuSoap extension has turned into a
I have a dynamic query which I call and I put the Result set
I have a MYSQL stored procedure SP1() that returns a result set. I want
I've got a result set I have to sort in quite a complex way.
I have a very complex Linq to SQL query that returns a result set

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.