I know that I am probably not going about this query in the most efficient manner, but I'm working with my limited knowledge to try to accomplish this task, so if you know of a more simplified way of writing this, please let me know.I have a bunch of income statement accounts that are coming in as year to date values, so I'm creating a temporary table that will sum the values for each account in the current period and and subtract the amounts (by account) in the prior periods to give me a periodic value for each month. Example: The value for an account in March (3) would subtract the values of that account for February(2). For February (2) it would subtract the year to date value for February from the year to date value from January. You would get theses values by using the following query with a change to the last variable:Select Time, Account, Entity, BusinessLine, Department, Value from dbo.TABLE_NAMEwhere time Like '201202'
This is what I have so far:Create table #TempTable ( Time1 int, Account nvarchar(200), Entity nvarchar(80), BusinessLine nvarchar (80), Department nvarchar (200), Value decimal (20,5) ) Goinsert into #TempTable ( Time1, Account, Entity, Businessline, Department, Value )Select Time, Account, Entity, BusinessLine, Department, Value from dbo.vw_Fact_Join_Writeback where time Like '2012%' and Currency='USD' and Value <> 0 and account between '430000' and '899999'goAlter Table #TempTable ADD "1" int, "2" int, "3" int, "4" int, "5" int,"6" int, "7" int, "8" int, "9" int, "10" int, "11" int, "12" intGoUpdate #TempTableSet "1" = Valuewhere time1 like '201201'Update #TempTableSet "2" = (select Valuewhere time1 like '201202') - "1"goselect * from #TempTablegoDrop table #TempTable
Thanks in advance for the help! I know that I'm making this a lot more difficult than it needs to be.