I’m a bit lost with the following problem that I need to solve with an SQL query, no plsql. The idea is to build a cumulative column to calculate all previous months. The input table looks like
Month
1
2
3
..
24
I need build the following table :
Month Cum_Month
1 1
2 1
2 2
3 1
3 2
3 3
..
24 1
...
24 23
All this in SQL Server 2008, thanks in advance
You can do it like this:
master..spt_valuesis used to generate numbers, after numbers are generated result of the subquery is joined on the@tblto get the number of rows that corresponds to themonth. After thatROW_NUMBERis used to create appropriate ordinal numbers for each month.