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.
Author |
Topic |
SimonG
Starting Member
15 Posts |
Posted - 2010-02-15 : 07:18:17
|
Hi all,Looking for some guidance on the best way to achieve the following as I can't seem to do this without some programming in SQL2000 (and I am a rookie!);I have a DTS package that exports a set of data, and consolidates individual transactions into months of activity by contract. I cannot determine up front how many months/years of data this will produce nor how many contracts will be involved. Neither can I guarantee that there will be activity each concurrent month!;I end up with a consolidated dataset like this after my initial DTS;Contract,Year,Month,Total_CostCW0391, 2008, 5, 100.00CW0392, 2008, 5, 250.00CW0391, 2008, 6, 100.00CW0392, 2008, 6, 250.00etc..My goal is to end up with a table of data that contains the above and a new cumulative cost column;Contact,Year,Month,Total_Cost,Cumulative_CostCW0391, 2008, 5, 100.00, 100.00CW0392, 2008, 5, 250.00, 250.00CW0391, 2008, 6, 100.00, 200.00CW0392, 2008, 6, 250.00, 500.00etc..I'm pulling my hair out trying to work out the best way of achieving this - I'm not looking for the solution just some guidance on the best way to tackle the problem.Regards,Simon |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2010-02-15 : 07:34:22
|
Most probably you will have to write a correlated subquery in this case which will sum up total_cost column based on Year, month and contract. Something like this:select Contract,Year,Month,Total_Cost,(Select sum(total_cost) from tbl t2 where t2.contract = t1.contractand t2.year <= t1.year and t2.month <= t1.month) as cumulative_costfrom tbl t1 Harsh Athalyehttp://www.letsgeek.net/ |
|
|
SimonG
Starting Member
15 Posts |
Posted - 2010-02-16 : 07:51:31
|
Thanks for this post.It unfortunately breaks down when the new year starts;CW0391 2008 5 28262.0400 28262.0400CW0391 2008 6 46713.9600 74976.0000CW0391 2008 7 282300.5300 357276.5300CW0391 2008 8 124523.3400 481799.8700CW0391 2008 9 35392.6800 517192.5500CW0391 2008 10 15710.2600 532902.8100CW0391 2008 11 36213.8700 569116.6800CW0391 2008 12 12433.0800 581549.7600CW0391 2009 1 30311.5800 30311.5800CW0391 2009 2 -7136.3400 23175.2400CW0391 2009 3 -1.0000 23174.2400CW0391 2009 4 .0000 23174.2400CW0391 2009 5 1.0000 51437.2800CW0391 2009 6 -445.0000 97706.2400CW0391 2009 8 8537.0000 513067.1100Any ideas?Simon |
|
|
SimonG
Starting Member
15 Posts |
Posted - 2010-02-16 : 10:20:17
|
Found the best way was to use the transaction date, convert to last day of the month and then use the correlated subquery as proposed via the date.Thanks for the help.Simon |
|
|
|
|
|
|
|