This query provides Year to date numbers for both Price and Square Feet of the current year and the previous year to date. This is more like the Running Total of the current Year and the Previous year with respect to the weeks in this case from 1 through 7 and so on..... (week 7th of 2017 ended on 02/19/2017) of the current year and the previous year(week 7th of 2016 ended on 02/22/2016). The reason why I am using subqueries is because this is the only way I know to get around this situation. And of course if you think there is a shorter, viable alternative of executing this query, please advice.
Actual_Sale_Date
holds data on all of the seven days of the week but we cut off on Sunday that is why 2/22/2016 (Sunday ending 7th week of 2016) and 2/19/2017 (Sunday ending 7th week of 2017).
I tried "Actual_Sale_Date" = date_trunc('week', now())::date - 1
this function only returns the previous week data ending on the passed Sunday. I took a look at interval
since dateadd
does not exist in postgresql but could not get my ways around with it.
My query:
select (money(Sum("Price") / COUNT("Price"))) as "Avg_Value YTD",
Round(Avg("Price"/"Sq_Ft"),+2) as "Avg_PPSF YTD",
(select
(money(Sum("Price") / COUNT("Price"))) from allsalesdata
where "Actual_Sale_Date" >= '01/01/2016' AND "Actual_Sale_Date" < '02/22/2016'
and "Work_ID" = 'SO') AS "Last Year at this time Avg_Value",
(select Round(Avg("Price"/"Sq_Ft"),+2)
from allsalesdata
where "Actual_Sale_Date" >= '01/01/2016' AND "Actual_Sale_Date" < '02/22/2016'
and "Work_ID" = 'SO') AS "Last Year at this time Avg_PPSF"
from allsalesdata
where "Actual_Sale_Date" >= '01/01/2017' AND "Actual_Sale_Date" <'02/20/2017'
and "Work_ID" = 'SO'
Sample Data:
Price Sq_Ft Actual_Sale_Date Work_ID
45871 3583 01/15/2016 SO
55874 4457 02/05/2016 SO
88745 4788 02/20/2016 SO
58745 1459 01/10/2016 SO
88749 2145 01/25/2017 SO
74856 1478 01/25/2017 SO
74586 4587 01/31/2017 ABC
74745 1142 02/10/2017 SO
74589 2214 02/19/2017 SO
weeks
series requirements. Running total of the columns as the year progresses. – Jake Wagner"Actual_Sale_Date" = date_trunc('week', now())::date - 1
this function only returns the previous week data ending on the passed Sunday. I took a look atinterval
sincedateadd
does not exist in postgresql but could not get my ways around with it.For example, Total 1st week 2016 Total 1st week 2017?
Yes exactly but running total as we enter March, April, etc... – Jake Wagner