Let's not get too hung up on the columns, let's concentrate on selecting the appropriate rows.
Say we had a table named [SalesSummary] containing
Item FiscalYear Quarter TotalSales
-------- ---------- ------- ----------
bicycles 2011 1 100
bicycles 2011 2 200
bicycles 2011 3 300
bicycles 2011 4 400
bicycles 2012 1 500
bicycles 2012 2 600
bicycles 2012 3 700
bicycles 2012 4 800
ham 2011 1 10
ham 2011 2 20
ham 2011 3 30
ham 2011 4 40
ham 2012 1 50
ham 2012 2 60
ham 2012 3 70
ham 2012 4 80
If we wanted to create a query that showed the quarterly figures along with the total sales for the past 12 months (current quarter plus the previous 3 quarters) one might think that we would have to start messing around with IIf()
to handle the "wrap around" from one FiscalYear to the next. Fortunately, we don't, because we can just replicate the same data with "fake" quarters for the next FiscalYear: 2011_Q(4) becomes 2012_Q(0), 2011_Q(3) becomes 2012_Q(-1), and so on.
SELECT
Item,
FiscalYear,
Quarter,
TotalSales
FROM SalesSummary
UNION ALL
SELECT
Item,
FiscalYear + 1,
Quarter - 4,
TotalSales
FROM SalesSummary
returns
Item FiscalYear Quarter TotalSales
-------- ---------- ------- ----------
bicycles 2011 1 100
bicycles 2011 2 200
bicycles 2011 3 300
bicycles 2011 4 400
bicycles 2012 1 500
bicycles 2012 2 600
bicycles 2012 3 700
bicycles 2012 4 800
ham 2011 1 10
ham 2011 2 20
ham 2011 3 30
ham 2011 4 40
ham 2012 1 50
ham 2012 2 60
ham 2012 3 70
ham 2012 4 80
bicycles 2012 -3 100
bicycles 2012 -2 200
bicycles 2012 -1 300
bicycles 2012 0 400
bicycles 2013 -3 500
bicycles 2013 -2 600
bicycles 2013 -1 700
bicycles 2013 0 800
ham 2012 -3 10
ham 2012 -2 20
ham 2012 -1 30
ham 2012 0 40
ham 2013 -3 50
ham 2013 -2 60
ham 2013 -1 70
ham 2013 0 80
If we save that query as [SalesUnion] then we can use it to produce our totals like so
SELECT
Item,
FiscalYear,
Quarter,
TotalSales,
(
SELECT
SUM(su.TotalSales)
FROM SalesUnion su
WHERE su.Item = s.Item
AND su.FiscalYear = s.FiscalYear
AND (su.Quarter Between s.Quarter - 3 And s.Quarter)
) AS SalesLast12Months
FROM SalesSummary s
Or, if you don't want to rely on a saved query you can do it all in one go:
SELECT
Item,
FiscalYear,
Quarter,
TotalSales,
(
SELECT
SUM(su.TotalSales)
FROM
(
SELECT
Item,
FiscalYear,
Quarter,
TotalSales
FROM SalesSummary
UNION ALL
SELECT
Item,
FiscalYear + 1,
Quarter - 4,
TotalSales
FROM SalesSummary
) su
WHERE su.Item = s.Item
AND su.FiscalYear = s.FiscalYear
AND (su.Quarter Between s.Quarter - 3 And s.Quarter)
) AS SalesLast12Months
FROM SalesSummary s
Either way, the results are
Item FiscalYear Quarter TotalSales SalesLast12Months
-------- ---------- ------- ---------- -----------------
bicycles 2011 1 100 100
bicycles 2011 2 200 300
bicycles 2011 3 300 600
bicycles 2011 4 400 1000
bicycles 2012 1 500 1400
bicycles 2012 2 600 1800
bicycles 2012 3 700 2200
bicycles 2012 4 800 2600
ham 2011 1 10 10
ham 2011 2 20 30
ham 2011 3 30 60
ham 2011 4 40 100
ham 2012 1 50 140
ham 2012 2 60 180
ham 2012 3 70 220
ham 2012 4 80 260