1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| With MaTable (id, date_mvt, qt, id_art) as
(
select 5, CONVERT(date, '10/10/2013', 103), -102, 9 union all
select 13, CONVERT(date, '09/10/2013', 103), 100, 182 union all
select 14, CONVERT(date, '10/10/2013', 103), - 10, 182 union all
select 15, CONVERT(date, '10/10/2013', 103), - 40, 182 union all
select 16, CONVERT(date, '12/10/2013', 103), 80, 182 union all
select 12, CONVERT(date, '09/10/2013', 103), 20, 415 union all
select 17, CONVERT(date, '12/10/2013', 103), 30, 415
)
select t1.id, t1.date_mvt, t1.qt, t1.id_art, SUM(t2.qt) as cumul
from MaTable as t1
join MaTable as t2
on t1.id_art = t2.id_art
and t1.date_mvt >= t2.date_mvt
and t1.id >= t2.id -- Pour différencier les id 13 et 14 qui ont la même date
group by t1.id, t1.date_mvt, t1.qt, t1.id_art
order by t1.id_art asc, t1.id asc;
id date_mvt qt id_art cumul
----------- ---------- ----------- ----------- -----------
5 2013-10-10 -102 9 -102
13 2013-10-09 100 182 100
14 2013-10-10 -10 182 90
15 2013-10-10 -40 182 50
16 2013-10-12 80 182 130
12 2013-10-09 20 415 20
17 2013-10-12 30 415 50 |
Partager