好的,如果我理解您的评论,对于给定的CaseID
,您可以有多个记录,每个记录都有LegalStatusTypeID
该案例的唯一性,每个记录都有一个日期,并且每个记录的适用性LegalStatusTypeID
介于该记录LegalStatusDate
和为该案例输入的下一条记录之间年代LegalStatusDate
:
SELECT qrySub.CaseID, qrySub.LegalStatusDate, LegalStatus.LegalStatusDate AS
NextLegalStatusDate
FROM (
SELECT LegalStatus_2.LegalStatusID, LegalStatus_2.CaseID, LegalStatus_2.
LegalStatusTypeID, LegalStatus_2.LegalStatusDate, MIN(qryNext.
LegalStatusID) AS NextLegalStatusID
FROM LegalStatus AS LegalStatus_2
LEFT JOIN (
SELECT LegalStatusID, CaseID, LegalStatusTypeID, LegalStatusDate
FROM LegalStatus AS LegalStatus_1
) AS qryNext
ON LegalStatus_2.CaseID = qryNext.CaseID AND LegalStatus_2.LegalStatusID
< qryNext.LegalStatusID
GROUP BY LegalStatus_2.LegalStatusID, LegalStatus_2.CaseID, LegalStatus_2.
LegalStatusTypeID, LegalStatus_2.LegalStatusDate
HAVING (LegalStatus_2.LegalStatusTypeID = 62)
) AS qrySub
LEFT JOIN LegalStatus
ON qrySub.NextLegalStatusID = LegalStatus.LegalStatusID
WHERE (
qrySub.LegalStatusDate BETWEEN CONVERT(DATETIME, '2013-01-01 00:00:00'
, 102) AND CONVERT(DATETIME, '2013-07-01 00:00:00', 102)
) OR (
LegalStatus.LegalStatusDate BETWEEN CONVERT(DATETIME,
'2013-01-01 00:00:00', 102) AND CONVERT(DATETIME,
'2013-01-07 00:00:00', 102)
) OR (qrySub.LegalStatusDate < CONVERT(DATETIME, '2013-01-01 00:00:00', 102)
) AND (
LegalStatus.LegalStatusDate > CONVERT(DATETIME, '2013-01-07 00:00:00',
102)
)
您需要将 = 62 的记录连接到任何给定案例的下一条记录,然后使用该下一个案例的 ID 来获取= 62LegalStatusTypeID
适用的结束日期。LegalStatusTypeID
由于您谈论的是在您的日期范围内有 a =LegalStatusTypeID
62的案例,因此您需要开始日期在您的日期范围内,或结束日期在您的日期范围内(或两者),或者您的日期范围介于case's = 62 开始和结束日期。LegalStatusTypeID