开始表中的原始数据是这样的: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达到以下效果:
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

解决方案 »

  1.   

    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
      

  2.   

    更改为如下:
    create table tb(emp_Id varchar(10),happen_date datetime,sale int,sale_month_sum int)
    insert into tb values('E01',        '2012-01-01',  100  ,       0)
    insert into tb values('E01',        '2012-01-02',  200  ,       0)
    insert into tb values('E01',        '2012-01-03',  500  ,       0)
    insert into tb values('E01',        '2012-01-04',  700  ,       0)
    insert into tb values('E01',        '2012-01-05',  150  ,       0)
    insert into tb values('E01',        '2012-01-06',  0    ,       0)
    insert into tb values('E02',        '2012-01-02',  600  ,       0)
    insert into tb values('E02',        '2012-01-03',  0    ,       0)
    insert into tb values('E02',        '2012-01-04',  300  ,       0)
    insert into tb values('E02',        '2012-01-05',  800  ,       0)
    insert into tb values('E02',        '2012-01-06',  0    ,       0)
    goselect t.emp_Id,t.happen_date,t.sale , sale + (select sum(sale) from tb where emp_Id = t.emp_Id and happen_date <= t.happen_date) sale_month_sum from tb tdrop table tb/*
    emp_Id     happen_date                                            sale        sale_month_sum 
    ---------- ------------------------------------------------------ ----------- -------------- 
    E01        2012-01-01 00:00:00.000                                100         200
    E01        2012-01-02 00:00:00.000                                200         500
    E01        2012-01-03 00:00:00.000                                500         1300
    E01        2012-01-04 00:00:00.000                                700         2200
    E01        2012-01-05 00:00:00.000                                150         1800
    E01        2012-01-06 00:00:00.000                                0           1650
    E02        2012-01-02 00:00:00.000                                600         1200
    E02        2012-01-03 00:00:00.000                                0           600
    E02        2012-01-04 00:00:00.000                                300         1200
    E02        2012-01-05 00:00:00.000                                800         2500
    E02        2012-01-06 00:00:00.000                                0           1700(所影响的行数为 11 行)*/
      

  3.   

    再次更改为如下:
    create table tb(emp_Id varchar(10),happen_date datetime,sale int,sale_month_sum int)
    insert into tb values('E01',        '2012-01-01',  100  ,       0)
    insert into tb values('E01',        '2012-01-02',  200  ,       0)
    insert into tb values('E01',        '2012-01-03',  500  ,       0)
    insert into tb values('E01',        '2012-01-04',  700  ,       0)
    insert into tb values('E01',        '2012-01-05',  150  ,       0)
    insert into tb values('E01',        '2012-01-06',  0    ,       0)
    insert into tb values('E02',        '2012-01-02',  600  ,       0)
    insert into tb values('E02',        '2012-01-03',  0    ,       0)
    insert into tb values('E02',        '2012-01-04',  300  ,       0)
    insert into tb values('E02',        '2012-01-05',  800  ,       0)
    insert into tb values('E02',        '2012-01-06',  0    ,       0)
    goselect t.emp_Id,t.happen_date,t.sale , (select sum(sale) from tb where emp_Id = t.emp_Id and happen_date <= t.happen_date) sale_month_sum from tb tdrop table tb/*
    emp_Id     happen_date                                            sale        sale_month_sum 
    ---------- ------------------------------------------------------ ----------- -------------- 
    E01        2012-01-01 00:00:00.000                                100         100
    E01        2012-01-02 00:00:00.000                                200         300
    E01        2012-01-03 00:00:00.000                                500         800
    E01        2012-01-04 00:00:00.000                                700         1500
    E01        2012-01-05 00:00:00.000                                150         1650
    E01        2012-01-06 00:00:00.000                                0           1650
    E02        2012-01-02 00:00:00.000                                600         600
    E02        2012-01-03 00:00:00.000                                0           600
    E02        2012-01-04 00:00:00.000                                300         900
    E02        2012-01-05 00:00:00.000                                800         1700
    E02        2012-01-06 00:00:00.000                                0           1700(所影响的行数为 11 行)*/
      

  4.   

    select *,day_sale+ case when 
    (select SUM(day_sale)from monthScore where happen_date<m.happen_date and emp_Id=m.emp_Id) is null then 0 else 
    day_sale+(select SUM(day_sale)from monthScore where happen_date<m.happen_date and emp_Id=m.emp_Id)
    end
    from monthScore(nolock) m