MySQL Forums
Forum List  »  InnoDB

Re: Accumulated Sum
Posted by: Jay Pipes
Date: June 26, 2005 07:48AM

Yes. Join the table to itself using the >= operator in the ON expression. The second result set can be SUMmed to provide the aggregated totals, like so:

Assume:

CREATE TABLE some_table (
some_date DATETIME NOT NULL
, some_quantity INT NOT NULL
, PRIMARY KEY (some_date)
);

you can get the report you requested like so:

SELECT t1.some_date, t1.some_quantity, SUM(t2.quantity) as "Rolling Sum"
FROM some_table t1
INNER JOIN some_table t2
ON t1.some_date >= t2.some_date;

Jay Pipes
Community Relations Manager, North America, MySQL Inc.

Got Cluster? http://www.mysql.com/cluster
Personal: http://jpipes.com

Options: ReplyQuote


Subject
Views
Written By
Posted
7647
June 26, 2005 07:36AM
Re: Accumulated Sum
3455
June 26, 2005 07:48AM
3821
June 26, 2005 10:07PM
3533
June 26, 2005 11:16PM
2945
June 27, 2005 12:32AM
2742
June 27, 2005 01:06AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.