On Tue, Jun 17, 2003 at 07:50:08PM +0300, Primaria Falticeni wrote: > Why nobody answered me?
I didn't see your post. Very few people have the time to read everthing posted to this list. > 2. How could I make accumulated sums in MySQL (described in the actual > forwarded message) > > > ----- Original Message ----- > From: "Primaria Falticeni" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Tuesday, June 17, 2003 5:48 PM > Subject: How the Partial SUMs are made in MySQL > > > Hello, > > I need one complex query which makes partial sums on some dates. > > Let's get the structure of table rates: Day Date, Amount. > 01/05/2003 3,000 > 01/06/2003 2,000 > 01/09/2003 4,000 > > > > SELECT Day as DayF,SUM(Amount) as S_Am FROM rates GROUP BY Day > > gives the list of totals each one from a day: > DayF S_Am > 01/05/2003 3,000 > 01/06/2003 2,000 > 01/09/2003 4,000 > > > > I need the sum of the Amount from the beginning of the file until each > day...something like SUM(Amount FOR Day<DayF) on each day. > > DayF S_Am > 01/05/2003 3,000 > 01/06/2003 5,000 (I mean 3,000 on 01/05/2003 + 2,000 on 01/06/2003) > 01/09/2003 9,000 (I mean 4,000 on 01/09/2003 + 3,000 on 01/05/2003 + > 2,000 on 01/06/2003) In English, these are usually (but not always) called running totals, or running sums. SELECT r0.Day, SUM(r1.Amount) as S_Am FROM rates AS r0, rates AS r1 WHERE r1.Day <= r0.Day GROUP BY r0.Day This runs fine in vi, but I haven't tried it in MySQL. Some adjustment may be necessary. If you're going to do much of this, I suggest getting a copy of "SQL For Smarties" by Joe Celko, which is where I learned how to do running totals. Bob Hall -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]