从开始日期和结束日期开始,如何找到当前“有效”的记录?

时间:2017-04-10 23:27:18

标签: ms-access ms-access-2003

我的主数据库表在行上有一个开始日期和结束日期,表示某个试验开始或结束的时间。

因此,如果试验在被查询的时间段之前开始并结束,则将其排除。如果在查询的时间段之后已开始并结束试验,则也应将其排除在外。

但是,如果试验在该时间段内开始或结束,或在该时间段结束之后开始并在该时间段结束后结束,则应将其包括在内。

我一直在摸不着头脑,我无法解决这个问题。

这是我现有的查询,但似乎包括在查询的时间段后开始和结束的试用。

SELECT Count(Trials.Active) AS CountOfActiveTrials
FROM Trials
WHERE (((((Trials.DateStudyOpened)>[Forms]![Startup]![txtReportStartDate] And (Trials.DateStudyOpened)<[Forms]![Startup]![txtReportEndDate]) Or (Trials.DateStudyOpened)<[Forms]![Startup]![txtReportStartDate]) And Not (Trials.DateStudyOpened)>[Forms]![Startup]![txtReportEndDate]) AND (((Trials.DateStudyClosed)>[Forms]![Startup]![txtReportStartDate] And ((Trials.DateStudyClosed)<[Forms]![Startup]![txtReportEndDate]) Or (Trials.DateStudyClosed)>[Forms]![Startup]![txtReportEndDate]) Or (Trials.DateStudyClosed) Is Null));

1 个答案:

答案 0 :(得分:0)

这就是如何做到这一点,就像这样简单:

SELECT Count(Trials.Active) AS CountOfActiveTrials
FROM Trials
WHERE (((Trials.DateStudyOpened)<=[Forms]![Startup]![txtReportEndDate]) AND ((Trials.DateStudyClosed)>=[Forms]![Startup]![txtReportStartDate]));