MS Access 查询,IIF 函数返回错误 "No value given for one or more required parameters"

MS Access query, IIF function is returning error as "No value given for one or more required parameters"

我有 Access 查询,它执行 Union、group、get Maximum、Left Join 和 IIF(isNull) 函数。 我必须联合三个子表并按列 "mainid" 分组,从列 "time" 中获取最大值作为 "MaxSubTime",mainid 列由 mainTable.id 过滤。 然后我将 LEFT Join MainTable 应用到 Union-Group。 如果 MaxSubTime 为 Null,那么我将 MainTable.endtime 应用于 MaxSubTime 并将其作为 "MaxMainTableEndTime"。 到这里一切正常。

string myQry = "SELECT MainTable.id, MainTable.sttime, IIF(IsNULL(a.MaxSubTime), MainTable.endtime, a.MaxSubTime) AS MaxMainTableEndTime 
            FROM (MainTable LEFT JOIN (SELECT mainid, MAX(SubTime) AS MaxSubTime 
            FROM (SELECT Sub_table_1.mainid, Sub_table_1.time AS SubTime FROM Sub_table_1 
                  UNION SELECT Sub_table_2.mainid, Sub_table_2.time AS SubTime Sub_table_2 
                  UNION SELECT Sub_table_3.mainid, Sub_table_3.time AS SubTime Sub_table_3 GROUP BY mainid) AS a ON a.mainid = MainTable.id)                       
            WHERE MaxMainTableEndTime >= #" + DateTime.Now + "# ";

但是当我按 "MaxMainTableEndTime" 过滤最终查询时,我收到此错误 "No value given for one or more required parameters"。 我在哪里做我的股份?

我通过使用 "Having" 子句并稍微编辑我的代码解决了这个问题

string myQry = "SELECT MainTable.id, MainTable.sttime, a.MaxSubTime
FROM MainTable LEFT JOIN (SELECT mainid, MAX(SubTime) AS MaxSubTime
FROM(SELECT Sub_table_1.mainid, Sub_table_1.time AS SubTime FROM Sub_table_1 
UNION SELECT Sub_table_2.mainid, Sub_table_2.time AS SubTime Sub_table_2
UNION SELECT Sub_table_3.mainid, Sub_table_3.time AS SubTime Sub_table_3 
GROUP BY mainid HAVING MaxSubTime >= #" + DateTime.Now + "#)
AS a ON a.mainid = MainTable.id) WHERE MainTable.endtime >= #" + DateTime.Now + "# "