0

Table 1 : Employee

    EmpId  CreatedAt
    --------------------------
    100    2015-11-09 07:21:02
    200    2017-01-24 18:24:01
    300    2016-08-20 06:55:35

Table 2 : Account

    AccId   EmpID   Currency   CreatedAt
    -----------------------------------------------
    9000     100       USD     2017-04-20 19:40:55
    9001     200       USD     2017-04-20 19:40:55
    9002     100       EUR     2017-05-20 19:40:55
    9003     200       USD     2017-04-20 19:40:55
    9004     100       USD     2017-04-20 19:40:55

Table 3 : Transaction

  TrnsId   AccId Amount CreatedAt
  ------------------------------------------
  10       9000   3000  2017-04-25 19:40:55
  11       9001    500  2017-05-25 19:40:55
  12       9000   -200  2017-05-30 19:40:55
  13       9000   -500  2017-06-11 19:40:55

I need to calculate the ending balance for the account after each transaction entry. Assume that each account begins with a zero balance.

I have written this query:

SELECT 
    account.AccId, SUM(transaction.amount) AS [Ending Balance] 
FROM 
    account
JOIN 
    transaction ON transaction.AccId = account.AccId 
GROUP BY 
    account.AccId   

but the results returned by the query are not correct.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0