A family of Microsoft relational database management systems designed for ease of use.
I assume all the dates are Tuesdays, and you wish to correlate the previous, following and week-after-that values on Market, CouponName, Tactic, Type and Segment. Try this:
SELECT Market, CouponName, [Date], Tactic, [Type], Segment,
(SELECT TotalRedeemed
FROM TheTable As T2
WHERE T2.Market = T1.Market
AND T2.CouponName = T1.CouponName
AND T2.Tactic = T1.Tactic
AND T2.[Type] = T1.[Type]
AND T2.Segment = T1.Segment
AND T2.[Date] = T1.[Date]-7) AS WeekPrior,
TotalRedeemed AS CurrentWeek,
(SELECT TotalRedeemed
FROM TheTable As T2
WHERE T2.Market = T1.Market
AND T2.CouponName = T1.CouponName
AND T2.Tactic = T1.Tactic
AND T2.[Type] = T1.[Type]
AND T2.Segment = T1.Segment
AND T2.[Date] = T1.[Date]+7) AS FollowingWeek,
(SELECT TotalRedeemed
FROM TheTable As T3
WHERE T3.Market = T1.Market
AND T3.CouponName = T1.CouponName
AND T3.Tactic = T1.Tactic
AND T3.[Type] = T1.[Type]
AND T3.Segment = T1.Segment
AND T3.[Date] = T1.[Date]+14) AS WeekAfterThat
FROM TheTable As T2;
If the dates are not all Tuesdays, then you can return a week-starting date for any Date with the following function:
Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
' Returns 'week starting' Date for any Date
' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. varDate - optional Date value for which week starting
' Date to be returned. Defaults to current date
If IsMissing(varDate) Then varDate = VBA.Date
If Not IsNull(varDate) Then
WeekStart = DateValue(varDate) - Weekday(varDate, intStartDay) + 1
End If
End Function
and correlate the subqueries with the outer query on the return value of the function, e.g.
AND WeekStart(1,T3.[Date]) = WeekStart(1,T1.[Date]+14)) AS WeekAfterThat
I would recommend against using Date and Type as column names. These are the names of a function and statement respectively in Access and as such, are 'reserved words'. This is why I've enclosed them in square brackets in the SQL statement. However they are best avoided as object names.