Q:

How to write a SQL to cascadingly accumulate rows of data?

I have a work plan in DB as follows. For instance, the first row means that Team A has 10 hours' work to do in June (SP1006).

Team Sprint  WorkHours
A    1006    10
A    1007    20
A    1008    30
A    1009    40
B    1008    50
B    1009    60
B    1010    70

I want to derive it into the following form to show: at the end of each month, how many hours are left to be done, according to the work plan. As you see, the fields of the derived table depend on the original datatable too.

Team    SP1005  SP1006  SP1007  SP1008  SP1009  SP1010
A       100     90      70      40      0       0
B       180     180     180     130     70      0

I'm trying to do this on SQL Server 2000. This is really hard for me. Can someone give some help? Thanks a lot!
Edit: Or you can ignore the row-to-column transformation part, (which I know how to do,) just concentrate on the accumulation part.

A:

For the accumulation part use a cursor. Unless the result set is very small (maybe <100 rows to process) when you can get away with a triangular join like. 
SELECT t1.Team, t1.Sprint,g.wh-SUM(t2.WorkHours)
 FROM tbl t1 JOIN tbl t2 ON t1.Team = t2.Team AND t2.Sprint <= t1.Sprint
 JOIN 
   (SELECT SUM(WorkHours) AS wh, Team  FROM tbl GROUP BY Team) g ON t1.Team=g.Team
 GROUP BY t1.Team, t1.Sprint,g.wh

But as the number of rows grows the amount of work required by the triangular join grows exponentially.

