Microsoft Access 2010
我试图在一次访问次数小于或等于13个月的时间内获取用户的Max访问次数。 所以最后一次访问是在相隔不到13个月的访问序列内进行的。我想排除那些偶尔使用它的人(即相隔超过13个月)
我用这个来运行我的查询:
SELECT ID, AppointmentDate, Visit
FROM tblVisitQuestions t1
WHERE t1.Visit =
(SELECT Max(t2.Visit)
FROM tblVisitQuestions t2
WHERE t2.ID=t1.ID AND (DateDiff("m",[t1].[AppointmentDate],[t2].[AppointmentDate]) <= 13)
GROUP BY t2.ID)
它的工作除了它返回多个相同ID号的值。 不确定我的数据或查询中是否有错误。
Ex:表格中的示例数据
CR AppointmentDate Visit
1 15-Apr-05 0
1 15-Jul-05 1
1 16-May-06 2
1 06-Jun-06 3
1 19-Dec-06 4
1 11-Nov-11 5
1 31-Jan-12 6
2 08-Jun-04 0
2 17-Dec-04 1
2 05-Jul-05 2
2 06-Dec-05 3
2 06-Feb-09 4
2 19-Apr-11 5
查询后我想要的是什么(不是我现在得到的)
CR AppointmentDate Visit
1 19-Dec-06 4
2 06-Dec-05 3
我实际上得到了什么:
CR AppointmentDate Visit
1 19-Dec-06 4
1 31-Jan-12 6
2 06-Feb-09 4
2 19-Apr-11 5
我需要一种方法来选择我得到的最大值的最小值
任何帮助将不胜感激! 感谢
答案 0 :(得分:0)
对于每次访问,您可以通过以下方式在13个月内获得最大访问次数:
SELECT ID, AppointmentDate, Visit,
(SELECT Max(t2.Visit)
FROM tblVisitQuestions as t2
WHERE t2.ID = t1.ID AND (DateDiff("m",[t1].[AppointmentDate],[t2].[AppointmentDate]) <= 13)
) as MaxVisit
FROM tblVisitQuestions as t1;
如果您只希望访问号码为1,则添加where子句:
WHERE t1.Visit = 1;