Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Adding rows in same table for different period

Author  Topic 

yingchai
Starting Member

33 Posts

Posted - 2011-06-03 : 05:56:59
Hi, I need your expertise on this issue. Basically the reason to do this is to calculate YTD values.


For example, I have the following data:

org acc period amount
--------------------------
SSD ASSET 01/2011 10
SSD ASSET 02/2011 15
SSD ASSET 03/2011 20


Based on the example above, I intend to get the following result set:

org acc period amount
--------------------------
SSD ASSET 01/2011 10
SSD ASSET 02/2011 15
SSD ASSET 03/2011 20
SSD ASSET YTD(01/2011) 10
SSD ASSET YTD(02/2011) 25
SSD ASSET YTD(03/2011) 45

Any help you can provide with be greatly appreciated.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-03 : 06:01:12
select org acc period amount
from tbl
union all
select org acc period = 'YTD(' + period + ')', amount = (select sum(t2.amount) from tbl t2 where t2.period <= t.period)
from tbl t
order by period

But I'm guessing that's not what you really want.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -