1

我有一个查询,我一直在尝试改进以允许用户研究逐年比较信息。该查询工作正常,但我还没有弄清楚如何解决的数字分组问题。查询提示用户输入开始年份和结束年份,然后输入开始月份和结束月份。在 2-9 个月内一切正常,但是当使用 1、10、11 或 12 时,所有月份编号以 1 开头的字段都会出现。所以我要求今年和去年的第 1 个月和第 2 个月 我得到 1(ty) 1(ly) 2(ty) 2(ly) 10(ty) 10(ly) 11(ty) 11(ly) 12(ty) 12(ly)。如何只返回我请求的特定数字范围?

SELECT Month([EventLog]![Date]) & "/" & Year([EventLog]![Date]) AS [Date]
    ,EventLog.Type
    ,Count(EventLog.UserID) AS [Attendance Events]
    ,Year([EventLog]![Date]) AS [Year]
    ,Month([EventLog]![Date]) AS [Month]
FROM EventLog
WHERE (((EventLog.LogType) Like "Eve*"))
GROUP BY Month([EventLog]![Date]) & "/" & Year([EventLog]![Date])
    ,EventLog.Type
    ,Year([EventLog]![Date])
    ,Month([EventLog]![Date])
HAVING (((EventLog.Type) Like "Att*") 
    AND ((Year([EventLog]![Date])) Between [Enter Start Year:] And [Enter End Year:]) 
    AND ((Month([EventLog]![Date])) Between [Enter Start Month:] And [Enter End Month:]))
ORDER BY Month([EventLog]![Date]);
4

2 回答 2

1

您可以PARAMETERS在查询开始时包含一个子句,以通知数据库引擎您的参数是数字。然后,当您在查询中引用参数时,您不需要将它们(从文本)转换为数字,因为就数据库引擎而言,它们已经是数字。

你的 SQL 的开头看起来像这样......

PARAMETERS [Enter Start Year:] Short, [Enter End Year:] Short;
SELECT Month([EventLog]![Date]) ...
于 2013-11-05T14:32:04.170 回答
1

提示值是字符串,但正在与数值进行比较。尝试:

AND ((Year([EventLog]![Date])) Between CInt([Enter Start Year:] )
                               And CInt([Enter End Year:]) )
AND ((Month([EventLog]![Date])) Between CInt([Enter Start Month:] )
                                And CInt([Enter End Month:])))
于 2013-11-05T13:20:35.227 回答