Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Working with aggregating information between columns

$
0
0

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_NAME
where 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) ) Go insert 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' go Alter 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" int Go Update #TempTable Set "1" = Value where time1 like '201201' Update #TempTable Set "2" = (select Value where time1 like '201202' - Value where time1 like '201201'

) go select * from #TempTable go Drop table #TempTable

I'm really stuck with the subtracting the data between periods.  I could do this with another temp table, but I would like the query to do all of the calculations. 



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>