Я не очень уверен в своем ответе, потому что вопрос несколько расплывчатый. Если это не так, возможно, у вас возникнет идея.
DECLARE @dBaseMailingDate DATETIME;
SET @dBaseMailingDate = '05/23/2016';
SELECT ts.*
, Minus4.[Number of Studies Minus 4 Weeks]
, Minus13.[Number of Studies Minus 13 Weeks]
, Minus52.[Number of Studies Minus 52 Weeks]
FROM TStudy AS ts
LEFT JOIN (
SELECT ts1.lStudyID
, COUNT(*) AS [Number of Studies Minus 4 Weeks]
FROM TStudy AS ts1
WHERE ts1.dCompletedDate < DATEADD(WEEK, -4, @dBaseMailingDate)
AND ts1.lPeopleID IN (ts.lPeopleID)
GROUP BY ts1.lStudyID
) AS Minus4 ON ts.lStudyId = Minus4.lStudyID
LEFT JOIN (
SELECT ts2.lStudyID
, COUNT(*) AS [Number of Studies Minus 13 Weeks]
FROM TStudy AS ts2
WHERE ts2.dCompletedDate < DATEADD(WEEK, -13, @dBaseMailingDate)
AND ts2.lPeopleID IN (ts.lPeopleID)
GROUP BY ts2.lStudyID
) AS Minus13 ON ts.lStudyId = Minus13.lStudyID
LEFT JOIN (
SELECT ts3.lStudyID
, COUNT(*) AS [Number of Studies Minus 52 Weeks]
FROM TStudy AS ts3
WHERE ts3.dCompletedDate < DATEADD(WEEK, -52, @dBaseMailingDate)
AND ts3.lPeopleID IN (ts.lPeopleID)
GROUP BY ts3.lStudyID
) AS Minus52 ON ts.lStudyId = Minus52.lStudyID
WHERE ts.dMailingDate < @dBaseMailingDate
AND ts.lStudyID IN (
SELECT ta.lStudyID
FROM TAssignments AS ta
WHERE ta.lStudyTypeID IN (0, 3, 5)
)
AND ts.lPeopleID IN (1);