Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: Cummulative Sum


Message #1 by "Enzo Zaragoza" <enzaux@g...> on Wed, 20 Nov 2002 16:25:46 +0800
Please re-read the bit in Jeff's post about:

<quote>
: A running total needs to be computed typically over some time period.  You
: don't have to use time, but there needs to be some column which naturally
: orders the data.
<quote>

The important part is "there needs to be some column which naturally orders
the data" - this column (eg it could be your ID column) is used in the
T2.Transactiondate<= T1.TransactionDate criteria. Add an additional criteria
to the WHERE clause to restrict the resultset to the date you want.

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Enzo Zaragoza" <enzaux@g...>
Subject: [sql_language] RE: Cummulative Sum


:
: Thanks Jeff it worked!!!!! It is good if there is no record having the
same date but
: when there are transaction having the same date this doesn't apply.  If I
want to have a running
: sum on all of the transactions dated Nov 21 2002 for example, how would I
do it? I've tried
: adding Time to the Date field and it's ok.  But there was this friend of
mine that have an
: existing database and have data already in it and the date field doesn't
contain time so how
: would he resolve that?
:
: For ex,
:
: TranID     TranDate     TranAmount
: -----------------------------------
: 1         Nov 21, 2002    100
: 2         Nov 20, 2002    200
: 3         Nov 20, 2002    300
: 4         Nov 21, 2002    400
:
: And what's worst with this is that there are cases where in just like in
the example
: that TranID 002 (Nov 20) happened first before TranID 001 (Nov 21).  It is
may be because
: the user can intervene the TranDate and change it to what ever date they
want.
:
:
: Thanks,
:
: enzo c",)
:
:
:
: -----Original Message-----
: From: Jeff Mason [mailto:je.mason@a...]
: Sent: Wednesday, November 20, 2002 8:26 PM
: To: sql language
: Subject: [sql_language] RE: Cummulative Sum
:
:
: A running total needs to be computed typically over some time period.  You
: don't have to use time, but there needs to be some column which naturally
: orders the data.  Given that, then, and assuming a transaction table
: containing a date and amount, try:
:
: SELECT TransactionDate,
: (SELECT SUM(TranAmount) FROM Transactions AS T2
: WHERE T2.Transactiondate<= T1.TransactionDate) AS RunningBalance
: FROM Transactions AS T1;
:
: --
: Jeff Mason              Custom Apps, Inc.
: Jeff@c...
:
:
: -----Original Message-----
: From: Enzo Zaragoza [mailto:enzaux@g...]
: Sent: Wednesday, November 20, 2002 3:26 AM
: To: sql language
: Subject: [sql_language] Cummulative Sum
:
:
:
: Hello!  Is there anyway that I can create a cummulative sum in SQL?  My
: purpose of doing this
: is to create a running balance per transaction (somehow like a fibonacci
: series).
:
: Thanks,
:
: Enzo c",)
: YahooID: yackydidakdak
:
:
:
:
:
:
:
:


  Return to Index