math - payment amount tracking sql -


i had question asked of me on job interview, , can't seem find right way this. i'm looking answer if comes again time i'll have more of idea of should done.

table has 3 columns. id, customername, , amount

values (1, "someone", 20000)

table b has 3 columns. custid, date, payment. values

(1, 1/1/2014, 100) (1, 2/1/2014, 200) (1, 3/1/2014, 500) (1, 4/1/2014, 175) 

what want know after each payment has been made, remaining balance on account. output be:

customer name, payment amount, remaining balance

for each payment.

how go accomplishing since i've stumped of database friends , can't seem find info on google....

in databases support ansi standard cumulative sum syntax, do:

select a.customername, b.payment,        (a.amount - sum(b.payment) on (partition a.id order date)) remainingbalance tablea left outer join      tableb b      on a.id = b.id; 

in other databases, use correlated subquery.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -