Calculate Running total without ordered index

Reply

Join Date: Jul 2008
Posts: 3
Reputation: scholzr is an unknown quantity at this point 
Solved Threads: 0
scholzr scholzr is offline Offline
Newbie Poster

Calculate Running total without ordered index

 
0
  #1
Jul 27th, 2008
I am trying to find a way to calculate a running total of a field in my database, and while I have found a lot of good solutions, such as http://www.daniweb.com/forums/thread127644.html, they all require that I have a column which is ordered in the way that I want it to display. My problem, is that I do not have this. I am building a site where users can enter transactions on thier own, and they are not required to enter them in order. (see sample table below- the BALANCE column is what I am trying to calculate). I want to order them first by the date column, and then by the uniqueID if there are more than one for a given date.
  1. Table acount
  2. ---------------------------------------------------------------------- | | |
  3. uniqueID | date| amount| BALANCE
  4. ---------------------------------------------------------------------- | | |
  5. DB10006 | 05-3-08 | 233.00 | 233.00
  6. DB10007 | 05-3-08 | -33.00 | 200.00
  7. DB10005 | 05-4-08 | 40.00 | 240.00
  8. DB10004 | 05-5-08 | -20.00 | 220.00
  9. ----------------------------------------------------------------------------------
I have found the following code:
  1. SELECT uniqueID, DATE, amount, amount + coalesce( (
  2.  
  3. SELECT sum( amount )
  4. FROM acount b
  5. WHERE b.uniqueID < a.uniqueID ) , 0
  6. ) AS BALANCE
  7.  
  8. FROM acount a
  9. ORDER BY uniqueID
However, as I mentioned, I can not order the table by the uniqueID alone. I need to use the following order
  1. ORDER BY DATE ASC, uniqueID ASC
Does anyone know how to incorporate this into the above query, or know another way to do this?
Last edited by scholzr; Jul 27th, 2008 at 11:38 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Calculate Running total without ordered index

 
0
  #2
Jul 27th, 2008
hello,

I am glad that you have got inspired by my solutions.

Questions:

1. How is your uniqueID computed?
2. Does uniqueID correspond to the date?
3. Isn't there a contradiction in

WHERE b.uniqueID < a.uniqueID

and your example
  1. DB10006 | 05-3-08 | 233.00 | 233.00
  2. DB10007 | 05-3-08 | -33.00 | 200.00
  3. DB10005 | 05-4-08 | 40.00 | 240.00
  4. DB10004 | 05-5-08 | -20.00 | 220.00
because smallest uniqueID DB10004 corresponds to largest date 05-5-08 ? Also, you should use ISO date: year-month-day, for example 2008-05-30.

-----
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: scholzr is an unknown quantity at this point 
Solved Threads: 0
scholzr scholzr is offline Offline
Newbie Poster

Re: Calculate Running total without ordered index

 
0
  #3
Jul 27th, 2008
Thanks for the quick reply. First of all, I should note the table that I gave is only a sample of the table that I am actually using (actually adapted from the original post that I referenced). It was easier to just copy that and adapt it than try to explain everything in my table. I should have also noted that the sample table is ordered how I would like it to be ordered in the output.
Originally Posted by tesuji View Post
1. How is your uniqueID computed?
My unique ID is actually an autonumbered integer which is assigned when the user inserts a new transaction (in my table it is an actual integer with no letters).
Originally Posted by tesuji View Post
2. Does uniqueID correspond to the date?
No, the unique ID is a autonumber that is assigned in the order the transaction is inserted into the table. because the user can select the date that the transaction actually occured, these is not necessarily going to correspond.
Originally Posted by tesuji View Post
3. Isn't there a contradiction in[

WHERE b.uniqueID < a.uniqueID

and your example
  1. DB10006 | 05-3-08 | 233.00 | 233.00
  2. DB10007 | 05-3-08 | -33.00 | 200.00
  3. DB10005 | 05-4-08 | 40.00 | 240.00
  4. DB10004 | 05-5-08 | -20.00 | 220.00
because smallest uniqueID DB10004 corresponds to largest date 05-5-08 ?
In the example table that I gave yes. This is where my problem lies. The sample table is ordered how I want it to be. the example code is written as I found it. I am trying to figure out a way to adapt the code to allow me to obtain the results in the order of the sample table, but to do this, I need to somehow have
  1. WHERE b.DATE < a.DATE AND b.uniqueID < a.uniqueID
to match my necessary order. I just don't know if this is possible, and if so, how do I do it?
Originally Posted by tesuji View Post
Also, you should use ISO date: year-month-day, for example 2008-05-30.
Yes, this was a mistake in entering the dates into the sample table. in my actual DB table, they are in the ISO format. (Sorry about that)
Last edited by scholzr; Jul 27th, 2008 at 12:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Calculate Running total without ordered index

 
0
  #4
Jul 27th, 2008
Hello again,

The problem are duplicate date (fecha) values, why sum will be wrongly computed. To solve this, date and uniqueID must be combined in that order because date has higher priority than uniqueID (I think so). In the expression string(b.fecha, b.uniqueID) 1st, date and uniqueID will be converted to chars and 2nd, both chars will be concatenated, for example string(2008-03-05, '.', 10006) results in '2008-03-05.10006'. Now duplicates are impossible ! The following select statement
  1. SELECT uniqueID, fecha as "Date", Amount, amount + coalesce((SELECT sum(amount) FROM ozr b WHERE string(b.fecha, '.', b.uniqueID) < string(a.fecha, '.', a.uniqueID) ), 0) as sAmount, string("Date", '.', uniqueID) as "test only" FROM ozr a ORDER BY string("Date", '.', uniqueID);

gives the result you are asking for:

  1. uniqueID DATE Amount sAmount test only
  2. ----------------------------------------------------------
  3. 10006 2008-03-05 233.00 233.00 2008-03-05.10006
  4. 10007 2008-03-05 -33.00 200.00 2008-03-05.10007
  5. 10005 2008-04-05 40.00 240.00 2008-04-05.10005
  6. 10004 2008-05-05 -20.00 220.00 2008-05-05.10004

What you have to do is, you should find an appropriate function, like string(), what concatenates strings on your database, if MySql, CONCAT() might solve it. Possibly, you have to convert date type (b.fecha) and integer type (b.uniqueID) into varchars first. On my database system this task is done automatically.


-----
tesu
Last edited by tesuji; Jul 27th, 2008 at 3:37 pm.
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: scholzr is an unknown quantity at this point 
Solved Threads: 0
scholzr scholzr is offline Offline
Newbie Poster

Re: Calculate Running total without ordered index

 
0
  #5
Jul 27th, 2008
Thanks! I finally got it figured out. Here's what I ended up doing (to get it to work in MySQL 5.0):
  1. SELECT uniqueID, fecha as "Date", Amount, amount + coalesce((SELECT sum(amount) FROM ozr b WHERE CONCAT(b.fecha, b.uniqueID) < CONCAT(a.fecha, a.uniqueID) ), 0) as sAmount, CONCAT("Date", uniqueID) as "test only" FROM ozr a ORDER BY DATE, uniqueID;
Last edited by scholzr; Jul 27th, 2008 at 3:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 46
Reputation: chaosprime is an unknown quantity at this point 
Solved Threads: 4
chaosprime's Avatar
chaosprime chaosprime is offline Offline
Light Poster

Re: Calculate Running total without ordered index

 
0
  #6
Jul 29th, 2008
You should mark the thread solved so people don't waste time on it.
Chaos
Lost Souls: text based RPG
MUDseek: MUD gaming search
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MySQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC