简体   繁体   English

选择过去三个月的记录

[英]Selecting records from the past three months

I have 2 tables from which i need to run a query to display number of views a user had in the last 3 months from now. 我有2个表,我需要从中运行查询以显示用户从现在开始的最近3个月中的视图数。

So far I have come up with: all the field types are correct. 到目前为止,我想出了:所有字段类型都是正确的。

 SELECT dbo_LU_USER.USERNAME
 , Count(*) AS No_of_Sessions
 FROM dbo_SDB_SESSION 
   INNER JOIN dbo_LU_USER 
     ON dbo_SDB_SESSION.FK_USERID = dbo_LU_USER.PK_USERID
 WHERE (((DateDiff("m",[dbo_SDB_SESSION].[SESSIONSTART],Now()))=0 
   Or (DateDiff("m",[dbo_SDB_SESSION].[SESSIONSTART],Now()))=1 
   Or (DateDiff("m",[dbo_SDB_SESSION].[SESSIONSTART],Now()))=2))
 GROUP BY dbo_LU_USER.USERNAME;

Basically, the code above display a list of all records within the past 3 months; 基本上,以上代码显示了过去3个月内所有记录的列表; however, it starts from the 1st day of the month and ends on the current date, but I need it to start 3 months prior to today's date. 但是,它从一个月的第一天开始,到当前日期结束,但是我需要从今天的日期开始3个月开始。

Also to let you know this is SQL View in MS Access 2007 code. 还要让您知道这是MS Access 2007代码中的SQL视图。

Thanks in advance 提前致谢

Depending on how "strictly" you define your 3 months rule, you could make things a lot easier and probably efficient, by trying this: 根据您定义“三个月”规则的“严格程度”,您可以尝试以下操作,使事情变得更加轻松和高效:

SELECT dbo_LU_USER.USERNAME, Count(*) AS No_of_Sessions 
FROM dbo_SDB_SESSION 
INNER JOIN dbo_LU_USER 
ON dbo_SDB_SESSION.FK_USERID = dbo_LU_USER.PK_USERID 
WHERE [dbo_SDB_SESSION].[SESSIONSTART] between now() and DateAdd("d",-90,now())
GROUP BY dbo_LU_USER.USERNAME;

(Please understand that my MS SQL is a bit rusty, and can't test this at the moment: the idea is to make the query scan all record whose date is between "TODAY" and "TODAY-90 days"). (请理解,我的MS SQL有点生疏,目前无法测试:这个想法是使查询扫描日期在“ TODAY”和“ TODAY-90 days”之间的所有记录。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM