mysql accumulated Accumulated

Cumulative

First on the table structure:

CREATE TABLE `abc` (
  `jidu` int(11) NOT NULL AUTO_INCREMENT,
  `jine` int(11) DEFAULT NULL,
  PRIMARY KEY (`jidu`)
) ENGINE=InnoDB AUTO_INCREMENT=14270 DEFAULT CHARSET=utf8;

data:

The INSERT  the INTO `abc` (jidu``, `jine`) the VALUES ( ' . 1 ' , ' 100 ' );
 the INSERT  the INTO ` abc` (jidu` `,` jine`) the VALUES ( ' 2 ' , ' 200 is ' );
 the INSERT  the INTO `abc` (jidu``, `jine`) the VALUES ( ' . 3 ' , ' 300 ' );
 the INSERT  the INTO ` abc` (jidu` `,` jine`) the VALUES ( ' . 4 ' , '300');

The desired result for the quarter amount accumulated value of:

 

 

 

Here autocorrelation achieved by using offset, associated look effect

select * from abc a JOIN abc b ON a.jidu >= b.jidu;

 

 

 The following will be easier to achieve statement:

SELECT a.jidu, a.jine, SUM (b.jine) AS Leiji
 from ABC A the JOIN ABC B the ON a.jidu > = b.jidu 
 the GROUP  BY a.jidu the ORDER  BY jidu;

 

Regressive

Still above the table, the result of the difference between the amount you want for each quarter:

 

 

 Or autocorrelation, look at the related effects:

select * from abc a left JOIN abc b ON a.jidu = b.jidu+1 ORDER BY a.jidu;

 

 Implementation sql:

select a.jidu, a.jine, ifnull (a.jine - b.jine, a.jine) as the casino 
 from the ABC Left  Join ABC b ON a.jidu = b.jidu + 1  Order  BY a.jidu;

Bin, if wrong place, please correct me.

Guess you like

Origin www.cnblogs.com/wangb2/p/12125334.html