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)
 selecting yearly data and pivoting it

Author  Topic 

midavis
Starting Member

23 Posts

Posted - 2010-08-26 : 11:01:05
I need to select a multitude of data 15 years out then pivot it into another table for Reports.
I have 2 tables

Table1 ( key1 , key2, incDate )
Table2 ( key1 , key2, amount, paidDate)

key1 and key2 hold the relationship between these two tables.

I need to get all data from Table2 that starts from the incDate from Table1 and goes out 15 years. I will need to have a running total of the amount starting at year1( the incDate year) and going out 15 years from that. I can grab the data found with a simple group by, but how do I also continue my running total for the remaining years? After that I will need to pivot this data into another table with columns like year1, year2, year3, etc. I will need 1 row in this new table for every row from Table1.

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-26 : 11:43:13
see scenario 1 for running total

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

midavis
Starting Member

23 Posts

Posted - 2010-08-26 : 11:48:27
I did come up with a solution to cross into a numbers table and achieved setting up the correct years to update for each of my sets. I then updated the amounts and running totals. This is 3 processes and takes quite a while to do. I have about 170k rows and for 15 years that is quite a bit of rows to insert into a temp table before I pivot it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-26 : 11:51:33
you mean you pivot over 170 k rows?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

midavis
Starting Member

23 Posts

Posted - 2010-08-26 : 12:00:17
No to pivot over 2 million rows. Currently the longest time in my query is to cross join the nums table to get data from Table1 setup for 15 years.
Go to Top of Page
   

- Advertisement -