0

I have one ATM machine that has information below:

-   --Date-----|--Withdraw---|---CashLoad
-   01/15/13--|----10----------|-------300
-   01/16/13--|----20
-   01/17/13--|----50
-   01/18/13--|---120
-   01/19/13--|----20----------|-------400
-   01/20/13--|----60
-   01/21/13--|----80
-   01/22/13--|----50
-   01/23/13--|----90----------|------300

I want to calculate the end-of-day balance for that ATM, this balance equals to the CashLoad - accumulated Withdraw amounts of each day. If the ATM is reloaded, the process starts over again

Here is what I'm looking for:

-   --Date------|--Withdraw---|------CashLoad---|--------EOD_Balance
-   01/15/13---|----10----------|-------300-----------|-----------290
-   01/16/13---|----20----------|-----------------------|-----------270
-   01/17/13---|----50----------|-----------------------|-----------220
-   01/18/13---|---120---------|------------------------|----------100
-   01/19/13---|----20----------|-------400-----------|-----------380
-   01/20/13---|----60----------|-----------------------|-----------320
-   01/21/13---|----80----------|-----------------------|-----------240
-   01/22/13---|----50----------|-----------------------|-----------190
-   01/23/13---|----90----------|-------300-----------|-----------210

This is the query I am currently using:

select
    tmp1.atminternalid, tmp1.date,
    tmp1.CashLoad - tmp1.accum_disp as cashbalafterload
from mytable as tmp1 where SettlementDate = (select max(SettlementDate)
from DM_ADMIN.dbo.temptable1001 as tmp2
where tmp2.ATMInternalID = tmp1.atminternalid )
order by tmp1.atminternalid

How do I change my query to get the results I am looking for?

digitalextremist
  • 5,952
  • 3
  • 43
  • 62
  • What are you currently trying? – digitalextremist Jan 22 '14 at 22:15
  • I created another column called accum_Wtd then I tried to use the CashLoad to subtract those values. It doesn't work yet. select tmp1.atmid, tmp1.date, tmp1.CashLoad - tmp1.accum_wtd as cashbalance from mytable as tmp1 where Date = (select max(Date) from mytable as tmp2 where tmp2.ATMID = tmp1.ATMID ) order by tmp1.ATMID – user3221030 Jan 22 '14 at 22:21
  • Added the query to the question since it's vital to know. Welcome to SO! – digitalextremist Jan 22 '14 at 22:28

1 Answers1

0

SQL Server 2008 doesn't have a cumulative sum function. You can solve this with a correlated subquery:

select atm.*,
       (select sum(cashload) - sum(withdraw)
        from atm atm2
        where atm2.date <= atm.date
       ) as EOD_Balance
from atm;

EDIT:

Well, that does change the problem. You need to sum from the date of the previous cash load:

select atm.*,
       (select sum(cashload) - sum(withdraw)
        from atm atm3
        where atm3.date <= atm.date and
              atm3.date >= CashLoadDate
       ) as EOD_Balance
from (select atm.*,
             (select max(date)
              from atm atm2
              where atm2.date <= atm.date and
                    cashload > 0
             ) as CashLoadDate
      from atm
     ) atm;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks for the code, Gordon. However, i forgot to mention that before the new cash load, they took out the remaining cash first and then load the new amount into the ATM. For example: on 01/18/13, remaining cash was $100, on 01/19/13, they took away that $100, put in $400, and the EOD_Balance now $380 ($400-$20) not ($400+$100-$20) – user3221030 Jan 23 '14 at 16:44
  • I tried it but got a few error messages. Can you please help? Thank you, Gordon! Msg 107, Level 15, State 1, Line 7 The column prefix 'atm' does not match with a table name or alias name used in the query. Msg 4104, Level 16, State 1, Line 10 The multi-part identifier "atm.date" could not be bound. Msg 8155, Level 16, State 2, Line 14 No column name was specified for column 1 of 'atm'. Msg 207, Level 16, State 1, Line 4 Invalid column name 'date'. – user3221030 Jan 23 '14 at 17:09
  • @user3221030 . . . Here is an example of the code working in SQL Fiddle (http://www.sqlfiddle.com/#!3/d41d8/28810). This is exactly the same code in the answer. – Gordon Linoff Jan 23 '14 at 17:14
  • I got it, Gordon! Thank you so much for your help! – user3221030 Jan 23 '14 at 17:34