I'm trying to get a moving average for a set of real estate data that I can then graph, using MS SQL 2005.
Fields are: DateSold datetime, FloorSQM decimal(8,2), Price decimal(10,0)
What I need to do is get an average price per square metres of all the properties sold within each month as far back as we have data, which can be several years.
Therefore I need to Price/FloorSQM each property, and then apply a moving average to them for each month. Hope that makes sense.
I am completely stuck trying to figure this out, and googling endlessly is endlessly confusing.
I thought I found what I'm looking for in another post, but I keep getting "incorrect syntax near 'm'" when I try to compile it.
SELECT YEAR(DateSold) AS year,
MONTH(DateSold) AS month,
ROUND(Euros/CONVERT(DECIMAL(10,2),FloorSQM),2) as avg
FROM
(select s.*, DATE_ADD(DTE, INTERVAL m.i MONTH) DateSold, m.i
FROM Property s
cross join (select 0 i union select 1 union select 2 union select 3 union
select 4 union select 5 union select 6 union select 7 union
select 8 union select 9 union select 10 union select 11) m) sq
WHERE DateSold <= curdate()
GROUP BY YEAR(DateSold), MONTH(DateSold)
When I have this working, I then need to introduce a WHERE clause so I can limit the properties found to certain kinds (houses, apartments) and areas (New York, Los Angeles, etc)
Can anyone lend a hand please? Many thanks for your help.