提示查询中的月号

时间:2013-11-05 13:00:32

标签: sql ms-access ms-access-2003

我有一个查询,我一直在努力改进,以允许用户研究比较逐年的信息。查询工作正常,但数字分组问题除外我还没弄清楚如何解决。查询提示用户输入开始年份和结束年份,然后开始月份和结束月份。一切都运行良好的2-9个月,但是当使用1,10,11或12时,所有字段的月份编号从1开始出现。所以我要求今年和去年的第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]);

2 个答案:

答案 0 :(得分: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:])))

答案 1 :(得分:1)

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

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

PARAMETERS [Enter Start Year:] Short, [Enter End Year:] Short;
SELECT Month([EventLog]![Date]) ...