[thelist] extracting site "visits" from a table tracking page views (mysql)

Sarah Sweeney mr.sanders at designshift.com
Tue Aug 24 14:59:31 CDT 2004


I'm totally stumped by this query I'm trying to write. I currently have 
a table in my database that tracks the page views by registered users of 
the site. This is used to compile stats for the client such as frequency 
of site use by user country, user organization, etc. I've been asked now 
to compiled similar reports based on user "visits" rather than 
individual page views. I'm not exactly sure how to classify a "visit", 
but I'm thinking that I will say a new visit begins when a user accesses 
a page at least 4 hours after the last page view (this is debatable, but 
not the most important aspect of my question).

My question is, how do I select only those records from my stats table 
that have a timestamp of at least 4 hours more than the previous record 
for the client. I'm thinking something along these lines...

SELECT    COUNT(spcurr.UserID) AS UserCount, spcurr.Country
FROM      Stats_Pages spcurr, Stats_Pages spprev
WHERE     spcurr.UserID = spprev.UserID
AND       MAX(spprev.Timestamp) < DATE_SUB(spcurr.Timestamp, INTERVAL 4 
HOUR)
GROUP BY  sp.Country
ORDER BY  UserCount DESC

...but I know that the MAX() in the WHERE statement is not quite right. 
Any ideas on how this can be accomplished?

TIA

-- 
Sarah Sweeney  ::  Web Developer & Programmer
Portfolio  ::  http://sarah.designshift.com
Blog  ::  http://hardedge.ca
Family  ::  http://geekjock.ca


More information about the thelist mailing list