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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:43:01+00:00 2026-05-11T12:43:01+00:00

I need some advice in tackling a query. I can handle this in a

  • 0

I need some advice in tackling a query. I can handle this in a front-end application, however, due to design, I have to inplement this in the back-end. I have the following

  CREATE TABLE [dbo].[openitems](     [id] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,     [type] [char](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,     [date] [smalldatetime] NULL,     [amount] [decimal](9, 2) NULL,     [daysOpen] [smallint] NULL,     [balance] [decimal](9, 2) NULL ) ON [PRIMARY]     insert into openitems values('A12399','INV','2008-12-05',491.96,123)  insert into openitems values('A12399','INV','2008-12-12',4911.37,116)  insert into openitems values('A12399','INV','2008-12-05',3457.69,109)  

The table above have all open invoices for a customer. I need to apply a payment to these invoices starting from the oldest invoice (daysOpen column in the table). So if I have a $550.00 payment, I'll first apply it to the invoice with 123 daysOld, that's $491.96 -$500 (which leaves $8.04 to be applied to the next invoice... and so on), then update that record (balance column in table) to 0.00 and move to the next and apply the remaining. That would be $4911.37 - $8.04, which would leave $4903.33. Since there is no balance left to be applied, the loop exits.

The balance column should now read

 0 4903.33 3457.69 

Note: I need to do this for all customers in a table (around 10,000). A customer has an average of about 20 invoices open.

Thanks

  • 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. 2026-05-11T12:43:01+00:00Added an answer on May 11, 2026 at 12:43 pm

    Consider the following:

    a payment either applies in full to a balance, applies in part to a balance, or overpays a balance.

    Now, imagine that we could find, for any balance, the cumulative balance of invoices to date. Rather than imagine that, let’s do it:

    create view cumulative_balance as select a.*,    (select sum( balance )    from openitems b    where b.id = a.id and b.type = a.type and a.daysOpen >= a.daysOpen)   as cumulative_balance from openitems a; 

    Now we can find the first cumulative balance less than or equal to the payment, for any id and type, and store that, and daysOpen, and cumulative balance in server variables.

    Then we update all openItems with that id and type, where daysOpen <= the value we got, setting all those balances to zero.

    Then we find the first non-zero balance of that id and type, and set its balance to be it’s balance – (payment – the cumulative balance we stored). if there’s an overpayment, this balance will be correctly negative.

    With the correct query, you’ll be able to do the lookup and first update in one statement.

    There are two problems. One is that you can’t determine, of two or more alances with the same id and type and daysOpen, which should be paid first. Adding a unique id to your table would serve as a tie-breaker for those cases.

    Second is the need to save the cumulative balance to use it in the query for the second update. if you designed your table correctly, with a column for invoice_amount that wasn’t updated by payments, and a payment column that was, this would solve your problem.

    An even better refactoring would be to have two tables, one for invoices and one for payment: then a view could just do all the work, by comparing cumulative balances to cumulative payments, producing a list of unpaid balances or overpayments.

    In fact, I designed just such a system for a major mortgage guarantee company with the initials FM. It was a bit more complicated than what you have, in that balances were calculated from a number of formulas of amounts and percentages, and multiple payers (actually, insurers, this was for mortgages that had gone into default) had to be invoiced in a prescribed order according to other rules, per defauted mortgage.

    All of this was done in views, with a short (100 line or so) stored procedure that essentially did what I’ve outlined above: used a view that ordered the billing of invoices by these rules, applied payments (in the view), calculating what additional payments to invoice on what date to which insurer. The stored procedure then just generated invoices for the current date (which current date could be set, again using a view, to any date for testing purposes).

    (The irony is that I’d taken the job onteh promise I’d get to write C++; the only C++ I wrote used the Oracle and Sybase C APIs to transfer data from the Oracle system to the Sybase one.)

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

Sidebar

Related Questions

No related questions found

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.