MS Access:筛选查询以显示IIF truepart值为True的数据

时间:2017-11-30 17:01:36

标签: sql ms-access

我在MS Access中写了一个查询,其目的是比较两个不同字段中的时间,如果这些时间是0 - 20分钟,则另一个字段将显示是 - 否则该字段将显示否。

查询完全符合我的要求;但是,当我运行查询时,我只希望它显示ConnCheck值为Yes的行。以下是我创建的查询:

SELECT qryBusTrips.SignupName, qryBusTrips.[GO Station], qryBusTrips.Line, 
   qryBusTrips.[Line Direction], qryBusTrips.StopAbbr, qryBusTrips.StopName, 
   qryBusTrips.Time, tblSignUp.SignupName AS [GO Signup], 
   tblGoStations.Node, tblGoDepartures.DepartureTime, DateDiff("s",
   [tblGoDepartures.DepartureTime],[qryBusTrips.Time]) AS SecondsDiff, 
   IIf([SecondsDiff] Between 300 And 1200,"YES","NO") AS ConnCheck
FROM qryBusTrips, tblGoStations INNER JOIN (tblSignUp INNER JOIN 
   tblGoDepartures ON tblSignUp.SignID = tblGoDepartures.SignId) ON 
   tblGoStations.GoID = tblGoDepartures.GoID
WHERE (((qryBusTrips.Time)>#12/30/1899 12:0:0# And (qryBusTrips.Time)
   <#12/30/1899 16:0:0#));

我在ConnCheck专栏的Criteria字段中尝试写'= YES'。但是当我运行查询时,我收到一条错误消息“Enter Parameter Value:SecondsDiff”。

我不知道我做错了什么。请帮忙。

1 个答案:

答案 0 :(得分:0)

由于ConnCheck是一个字符串,请输入条件

='YES'

此外,SecondsDiff结果列在SELECT列表中的其他表达式的同一查询中不可用(这会生成错误)。你必须重复整个表达:

IIf(DateDiff("s",tblGoDepartures.DepartureTime,qryBusTrips.[Time]) Between 300 And 1200,
    "YES", "NO") AS ConnCheck

整个查询

SELECT
    qryBusTrips.SignupName,
    qryBusTrips.[GO Station],
    qryBusTrips.Line,
    qryBusTrips.[Line Direction],
    qryBusTrips.StopAbbr,
    qryBusTrips.StopName,
    qryBusTrips.Time,
    tblSignUp.SignupName AS [GO Signup],
    tblGoStations.Node,
    tblGoDepartures.DepartureTime,
    DateDiff("s", tblGoDepartures.DepartureTime, qryBusTrips.[Time]) AS SecondsDiff,
    IIf(DateDiff("s",tblGoDepartures.DepartureTime,qryBusTrips.[Time]) Between 300 And 1200,
        "YES","NO") AS ConnCheck
FROM
    qryBusTrips,
    tblGoStations INNER JOIN (
        tblSignUp INNER JOIN tblGoDepartures ON tblSignUp.SignID = tblGoDepartures.SignId
    ) ON tblGoStations.GoID = tblGoDepartures.GoID
WHERE
    qryBusTrips.Time > #12/30/1899 12:0:0# And
    qryBusTrips.Time < #12/30/1899 16:0:0#;

查询qryBusTrips未明确加入。这会创建隐式SQL Cross Join,因此会生成Cartesian Product。这是你想要的吗?