views:

147

answers:

1

Hi Everyone,

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.

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; 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.

Also to let you know this is SQL View in MS Access 2007 code.

Thanks in advance

A: 

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").

p.marino
Thanks, that did it :D
Khalid
i think you could get a performance improvement by changing `between now() and ` to simply `>`, since access will otherwise do two checks, i.e. for both boundaries. since sessionstart will never be in the future, you only need to check the `> -90` part, not the `< now()`
David Hedlund
That's yet another interpretation of 3 months, i.e., 90 days ago!
David-W-Fenton