Я бы начал с транспонирования приглашений и кликов. Затем выполняем сравнение дат. Вот как транспонировать, используя CTE и PIVOT в SQL Server:
WITH cteTStudy AS (
SELECT lPeopleID,dInvite,
ROW_NUMBER() OVER(PARTITION BY lPeopleID ORDER BY dInvite) AS InviteNumber
FROM TStudy
), cteTClicks AS (
SELECT lPeopleID,dLogDate ,
ROW_NUMBER() OVER(PARTITION BY lPeopleID ORDER BY dLogDate ) AS ClickNumber
FROM TClicks
), cteTStudyPvt AS (
SELECT lPeopleID,[1] AS Invite1, [2] AS Invite2, [3] AS Invite3
FROM
(
SELECT lPeopleID,dInvite,InviteNumber
FROM cteTStudy
) AS SourceTable
PIVOT
(
MAX(dInvite) FOR InviteNumber IN ([1], [2], [3])
) AS PivotTable
), cteTClicksPvt AS (
SELECT lPeopleID,[1] AS Click1, [2] AS Click2, [3] AS Click3
FROM
(
SELECT lPeopleID,dLogDate,ClickNumber
FROM cteTClicks
) AS SourceTable
PIVOT
(
MAX(dLogDate) FOR ClickNumber IN ([1], [2], [3])
) AS PivotTable
)
SELECT i.lPeopleID,Invite1,Invite2,Invite3,Click1,Click2,Click3,p.lremoved
FROM cteTStudyPvt AS i LEFT JOIN cteTClicksPvt as c ON i.lPeopleID = c.lPeopleID LEFT JOIN TProfile p ON i.lPeopleID = p.lPeopleID
Затем напишите код для сравнения между Пригласить1 и Клик1 и т. Д.