日期:2014-05-18  浏览次数:20449 次

一个非常难搞的sql查询,高手请进
开始表中的原始数据是这样的:
SQL code

emp_Id     happen_date sale        sale_month_sum
---------- ----------- ----------- --------------
E01        2012-01-01  100         0
E01        2012-01-02  200         0
E01        2012-01-03  500         0
E01        2012-01-04  700         0
E01        2012-01-05  150         0
E01        2012-01-06  0           0
E02        2012-01-02  600         0
E02        2012-01-03  0           0
E02        2012-01-04  300         0
E02        2012-01-05  800         0
E02        2012-01-06  0           0



通过一个sql达到以下效果:

SQL code

emp_Id     happen_date sale        sale_month_sum
---------- ----------- ----------- --------------
E01        2012-01-01  100         100
E01        2012-01-02  200         300
E01        2012-01-03  500         800
E01        2012-01-04  700         1500
E01        2012-01-05  150         1650
E01        2012-01-06  0           1650
E02        2012-01-02  600         600
E02        2012-01-03  0           600
E02        2012-01-04  300         900
E02        2012-01-05  800         1700
E02        2012-01-06  0           1700



------解决方案--------------------
探讨
引用:
select t.* , sale + (select sum(sale) from tb emp_Id = t.emp_Id and happen_date < t.happen_date) sale_month_sum from tb t
更改为如下:

SQL code
create table tb(emp_Id varchar(10),h……