I am trying to build a query in Postgresql that will be used for the budget.
I currently have a list of data grouped by month.
For every month of the year I need to get the average monthly sales for the previous three months. For example, in January, I would need average monthly sales from October to December last year. So the result would be something like this:
1 12345.67 2 54321.56 3 242412.45
This is grouped by month number.
Here is a snippet of code from my request that will give me sales this month:
LEFT JOIN (SELECT SUM((sti.cost + sti.freight) * sti.case_qty * sti.release_qty) AS trsf_cost, DATE_PART('month', st.invoice_dt) as month FROM stransitem sti, stocktrans st WHERE sti.invoice_no = st.invoice_no AND st.invoice_dt >= date_trunc('year', current_date) AND st.location_cd = 'SLC' AND st.order_st != 'DEL' GROUP BY month) as trsf_cogs ON trsf_cogs.month = totals.month
I need another connection that will give me the same, only averaged from the previous 3 months, but I'm not sure how to do it.
This will ALWAYS be January-December (1-12), starting in January and ending in December.
source share