-1

实际表

YEAR  TOT
----  ---
2001    1
2002    2
2003    3
2010   10
2011   11
2012   12
2013   13
2014   14
2015   15

现在预期结果:

YEAR  TOT  Exp_res
----  ---  -------------------
2001    1  1+2+3+4+5      = 15
2002    2  2+3+4+5+6      = 20
2003    3  3+4+5+6+7      = 25
2010   10  10+11+12+13+14
2011   11  11+12+13+14+15 
2012   12  12+13+14+15 
2013   13  13+14+15 
2014   14  14+15 
2015   15  15
4

1 回答 1

1

limit我认为您可以使用子句对相关子查询执行您想要的操作:

select id, tot,
       (select sum(tot)
        from t t2
        where t2.id >= id
        order by t2.id
        limit 5
       ) as Exp_Res
from t;
于 2013-09-18T11:39:15.577 回答