0

我正在尝试legalStatus从下表布局中查找特定日期范围的特定内容。

CREATE TABLE dbo.LegalStatus (
LegalStatusID int IDENTITY,
CaseID int NOT NULL,
LegalStatusTypeID int NOT NULL,
LegalStatusDate smalldatetime NOT NULL

示例:我可能有三个状态记录。

LegalStatusID = 1,
CaseID =17,
LegalStatusTypeID = 52,
LegalStatusDate = 4/1/12

LegalStatusID = 2,
CaseID =17,
LegalStatusTypeID = 62,
LegalStatusDate = 10/1/12

LegalStatusID = 3,
CaseID =17,
LegalStatusTypeID = 72,
LegalStatusDate = 10/1/13

我正在尝试报告在 2013 年 1 月1 日到 2013 年 7 月LegalStatusTypeID1 日之间有 = 62 的所有案例。如果有结束日期,这将很容易。帮助!安迪

4

3 回答 3

1

好的,如果我理解您的评论,对于给定的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

于 2013-10-11T00:31:17.703 回答
0
  SELECT *

  FROM   LegalStatus
  WHERE  LegalStatusTypeID = 62 AND
         LegalStatusDate BETWEEN '01/01/13' and '07/01/13'
于 2013-10-10T22:14:27.203 回答
0

我将您的问题理解为试图列出所有 CaseID 值,其中案件的法律状态在从“20130101”开始的整个期间保持为 62,并上升到但不包括“20130701”。案例在给定日期的法律状态由该案例在给定日期之前或当天的最新 LegalStatusTypeID 值确定。

如果这就是你想要的,我认为这可能会做到。

declare @fromD smalldatetime = '20130101';
declare @uptoD smalldatetime = '20130701';
with Cases as (
  select distinct CaseID
  from LegalStatus
)
  select CaseID
  from Cases
  where (
    select top (1) LegalStatusTypeID
    from LegalStatus as L
    where L.CaseID = Cases.CaseID
    and LegalStatusDate <= @fromD
    order by LegalStatusDate desc
  ) = 62 and not exists (
    select *
    from LegalStatus as L
    where L.CaseID = Cases.CaseID
    and LegalStatusDate > @fromD
    and LegalStatusDate <= @uptoD
    and LegalStatusTypeID <> 62
  )

这将选择“20130101”上的状态为 62 并且在“20130101”之后但“20130701”之前没有后续状态更改为不同于 62 的值的 CaseID 值。如果那不是您想要的,也许这仍然会有所帮助。此查询假定案例的法律状态每天只能更改一次,即 (CaseID,LegalStatusDate) 是您的表的候选键。

我没有对它进行太多测试,因为您提供的样本数据太少来解释您想要什么。

更新:原始海报在下面的评论中澄清了这个问题。以下查询应识别在指定时间段内的任何时间点(即使只是一天),案例的法律状态为 62 的那些 CaseID 值。它选择在“20130101”上状态为 62 的那些案例以及在该期间的任何时间点状态变为 62 的那些案例。

with Cases as (
  select distinct CaseID
  from LegalStatus
)
  select CaseID
  from Cases
  where (
    select top (1) LegalStatusTypeID
    from LegalStatus as L
    where L.CaseID = Cases.CaseID
    and LegalStatusDate <= @fromD
    order by LegalStatusDate desc
  ) = 62 or exists (
    select *
    from LegalStatus as L
    where L.CaseID = Cases.CaseID
    and LegalStatusDate > @fromD
    and LegalStatusDate < @uptoD
    and LegalStatusTypeID = 62
  )
于 2013-10-11T00:58:27.843 回答