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)
 Confusing Calculation Query

Author  Topic 

silver_surfer21
Starting Member

21 Posts

Posted - 2011-03-01 : 10:31:55
Hi,

I have a database of data about properties, when they were sold and how much they went for, which we are using to predict the current price of that property. In order to do this I have constructed the following query

SELECT [Postcode],c.Postout,[House Type], Year, Avg_monthly_increase, ID, decPrice,Application_Date, NewBuild,LeaseStatus,SAON,PAON,Thoroughfare, Locality, Town, District, County, Easting, Northing, strAddress FROM(SELECT Postout,[House Type] , Year, AVG(Percentage_Increase) AS Avg_Monthly_Increase FROM (SELECT a.Postout, a.[House Type], a.Year, a.Month, a.Average_Price, a.Min_Price, a.Max_Price, ((a.Average_Price-b.Average_Price)/b.Average_Price)*100  AS Percentage_Increase FROM [HouseData] AS a INNER JOIN [HouseData] AS b ON a.Postout = b.Postout WHERE a.[House Type] = b.[House Type] AND((a.YEAR = b.YEAR AND a.Month = b.Month+1)  OR (a.year = b.Year+1 AND a.Month = 1 AND b.MONTH = 12))) AS Query GROUP BY Postout, [House Type], Year) AS c INNER JOIN Postcode_DateSplit ON Postcode_DateSplit.Postout = c.Postout WHERE c.[House Type] = Postcode_Datesplit.[HouseType] AND c.Year=YEAR(Postcode_DateSplit.Application_Date)


which provides details of each property along with the increase in value per month for a house of that type, in that postal region in a specific year.

From this I now need to calculate, for each property, its current value, and the difference between that and the value when it was sold, by multiplying the original sale value by the appropriate percentage for each month since the original sale.
For example, if a house was sold in November 2009 and the average value increase per month was 2.76% then I would multiply the sale price by 2.76% twice. If in 2010 the average value increase per month became 3.2% then I would multiply the value by 3.2% 12 times and so on until reaching the estimated current value.

The problem I am having is the best way to turn this into a query, or otherwise calculate the results for a large number of records. Any suggestions would be appreciated.

Thanks.

lappin
Posting Yak Master

182 Posts

Posted - 2011-03-28 : 05:23:17
Possible suggestion is to use you code above to build a table / View which has Monthly Change for each postcode and type:
CREATE VIEW vwMonthlyChange
AS
Select [Year],[Month],[Postcode],[HouseType], [Pricechange]
FROM ....(use your existing code)

Would have data similar to:
2011, 1, NW1,Detached,-5
2011,2, NW1,Detached,2

Then use this to get a change between dates for house type in postcode.
Select SUM([Pricechange]]
from vwMonthlyChange
where [Year] ='2011' and ([Month]>='1' and [Month]<'3')
and [Postcode]='NW1' and [HouseType] ='Detached'


Go to Top of Page

silver_surfer21
Starting Member

21 Posts

Posted - 2011-03-30 : 04:40:07
Thanks, that works well. :)
Go to Top of Page
   

- Advertisement -