In response to your update - your formula is wrong. You're looking for:
SELECT <total_time_in_system> / <number_of_times_in_system>
FROM . . .
So, given a table that looks something like:
mysql> describe timestamps;
SESSION INT
CUSTOMER INT
TIME TIMESTAMP
ENTRY TINYINT
. . .
mysql>
With the constraints
- (SESSION, ENTRY) is unique
- ENTRY is constrained to (0,1).
you could write SQL like
SELECT SUM(UNIX_TIMESTAMP(b.time) - UNIX_TIMESTAMP(a.time))
FROM timestamps a, timestamps b
WHERE a.session = b.session
AND a.entry = 1
AND b.entry = 0
;
SELECT COUNT( * )
FROM timestamps
WHERE entry = 1
;
Divide the first by the second, checking to make sure that the second isn't 0. You now have the average number of seconds any given login spent on your site.
Of course, I'd make sure that the appropriate indices were there. Maybe, an index on (SESSION, ENTRY, TIME) would be appropriate.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
|