in reply to MySql Timestamps

Also, make sure you timestamp is long enough so that you're getting as specific as your needs dictate. Here's some info on timestamp definitions if you're not familiar. As dragonchild suggested, the manual is always a good place to start.

Updated in response:
Something along these lines might help...
SELECT (SUM(exittime) - SUM(starttime)) / COUNT(*) AS average FROM cust_records;

Thnx to dragonchild for pointing out my incorrect logic :)
~hb

Replies are listed 'Best First'.
Re^2: MySql Timestamps
by dragonchild (Archbishop) on Jul 06, 2004 at 21:34 UTC
    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