I Have these tables. I need to select the InvoiceDueDate in overDue, the logic is:
PendingCredit = Amount alread paid
Select which InvoiceDueDate doesn't cover the amount already paid.
Something like:
var table1 = select * 
             from Table1 
             order by InvoiceDueDate 
             where Balance > 0 
             and ReceivableId = 72
var pending = 940.13
foreach(var record in table1)
{
   pending = pending - balance
   if(pending <= 0)
      return InvoiceDueDate
}
Table 1
╔══════════════╦═══════════════╦═════════╦════════════════╗
║ ReceivableId ║ PendingCredit ║ Balance ║ InvoiceDueDate ║
╠══════════════╬═══════════════╬═════════╬════════════════╣
║           72 ║ 940.13        ║ 183.79  ║ 21/10/2014     ║
║           72 ║ 940.13        ║ 90.87   ║ 27/10/2014     ║
║           72 ║ 940.13        ║ 160.55  ║ 28/10/2014     ║
║           72 ║ 940.13        ║ 92.03   ║ 3/11/2014      ║
║           72 ║ 940.13        ║ 200.9   ║ 4/11/2014      ║
║           72 ║ 940.13        ║ 15.02   ║ 6/11/2014      ║
║           72 ║ 940.13        ║ 34.96   ║ 6/11/2014      ║
║           72 ║ 940.13        ║ 108.15  ║ 10/11/2014     ║
║           72 ║ 940.13        ║ 27.48   ║ 17/11/2014     ║
║           72 ║ 940.13        ║ 26.38   ║ 1/12/2014      ║
╚══════════════╩═══════════════╩═════════╩════════════════╝