如何累计求和

【问题】

I have SQL table which has 3 columns like this

entry_time          |  A        | B      |

-------------------------------------------

2015-06-05 18:44:56 |  10       |  8     |

2015-06-06 16:04:36 |  -5       | -2     |

2015-06-07 10:01:14 |   3       |  1     |

I want to get the sum of A and B columns up to particular given time stamp like below.

 status at given date |  A        | B      |

 -------------------------------------------

 2015-06-05 23:59:59 |  10       |  8     |

 2015-06-06 23:59:59 |   5       |  6     |

 2015-06-07 23:59:59 |   8       |  7     |

I thought using of between. But as I am giving the date I want totals from front end without seeing of data table and its first records date, is there any way to solve this.Please help me.

 

有人给出答案:

select

entry_time,

a_sum as A,

b_sum as B from(

  select

  entry_time,

  @A_sum:= @A_sum + A as a_sum,

  @B_sum:= @B_sum + B as b_sum

  from table_name,(select @A_sum:=0,@B_sum:=0)x

  order by entry_time

)x

 

【回答】

上面通过变量,sql实现很容易,下面是SPL的实现方法,跨行累计更直观:


A

1

$ select entry_time,A,B from tb order by entry_time

2

=A1.run(A=A[-1]+A,B=B[-1]+B)

A1sql取数,按照entry_time排序

A2:跨行累加,对AB列重新赋值