0

当我尝试 IIF 语句时,我无法让 Where 语句正常工作。我尝试了一些 sql 变体,但无济于事。我想做的是计算假期增益到某个日期。当我单独使用我的 Between 语句时,它可以正常工作。当我在系统时钟超过 13 年 5 月 20 日之后尝试重置计数器时,当它应该为刚刚过去的阵亡将士纪念日假期显示 1 时,我没有返回任何结果。我无法弄清楚正确语法的代码段开是

WHERE (((SchedulingLog.EventDate)=IIf(Date()>DateSerial(Year(Date()),5,20),((SchedulingLog.EventDate) Between DateSerial(Year(Date()),5,20) And DateSerial(Year(Date())+1,5,20)),((SchedulingLog.EventDate) Between DateSerial(Year(Date())-1,5,20) And DateSerial(Year(Date()),5,20)))))

下面是我的整个sql。

SELECT Roster.UserID, SchedulingLog.Category, 
Sum(IIf([CatDetail] Like '*Gain*',[Value],0)) AS Gain, DateSerial(Year(Date()),5,20) AS ResetDate, Date() AS [Date]
FROM SchedulingLog INNER JOIN Roster ON SchedulingLog.UserID = Roster.UserID
WHERE (((SchedulingLog.EventDate)=IIf(Date()>DateSerial(Year(Date()),5,20),((SchedulingLog.EventDate) Between DateSerial(Year(Date()),5,20) And DateSerial(Year(Date())+1,5,20)),((SchedulingLog.EventDate) Between DateSerial(Year(Date())-1,5,20) And DateSerial(Year(Date()),5,20)))))
GROUP BY Roster.UserID, SchedulingLog.Category, DateSerial(Year(Date()),5,20), Date()
HAVING (((SchedulingLog.Category) Like "DH*" Or (SchedulingLog.Category) Like "Comp*"));

任何帮助将不胜感激。

4

1 回答 1

2

我相信问题是双重的,首先你要寻找你的 DATE = BETWEEN Date and Date 的位置。不确定这是否是 Access 中的问题,语法通常是日期和日期之间的日期。第二个问题是尝试在 IIF 语句中返回命令,而不是返回要由 BETWEEN 语句评估的值。

我相信这应该有效:

SELECT Roster.UserID, SchedulingLog.Category, 
Sum(IIf([CatDetail] Like '*Gain*',[Value],0)) AS Gain, DateSerial(Year(Date()),5,20) AS ResetDate, Date() AS [Date]
FROM SchedulingLog INNER JOIN Roster ON SchedulingLog.UserID = Roster.UserID
WHERE SchedulingLog.EventDate BETWEEN IIf(Date()>DateSerial(Year(Date()),5,20)
                                                ,DateSerial(Year(Date()),5,20)
                                                ,DateSerial(Year(Date())-1,5,20))
                             AND IIf(Date()>DateSerial(Year(Date()),5,20)
                                                ,DateSerial(Year(Date())+1,5,20)
                                                ,DateSerial(Year(Date()),5,20))
GROUP BY Roster.UserID, SchedulingLog.Category, DateSerial(Year(Date()),5,20), Date()
HAVING (((SchedulingLog.Category) Like "DH*" Or (SchedulingLog.Category) Like "Comp*"));
于 2013-05-29T17:55:44.130 回答