Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3235066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:27:50+00:00 2026-05-17T17:27:50+00:00

Would it be possible to calculate time online from a table of something like

  • 0

Would it be possible to calculate time online from a table of something like

logtype   time  
----------------
login     2:30  
logout    2:45  
login     3:20  
logout    4:50  
login     5:00  
login     5:10  
logout    6:00  

It would have extra logins because sometimes the server crashes and doesn’t add logout.
Can you do something like this in MySQL only or would you have to use possibly ColdFusion?
And if so, how would you do it in MySQL/ColdFusion?

All I’m wanting is a (rough) total of time online, hours, days, minutes etc for a particular user.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:27:50+00:00Added an answer on May 17, 2026 at 5:27 pm

    As others have suggested, modifying the table structure is a better approach in the long run. Trying to pair up log records (with no matching key) is difficult, and sometimes impossible. A slightly modified structure would be easier to query, and would yield more accurate results. However, if you absolutely cannot modify the table, it might be possible to calculate a rough total – IF your record ID is numeric/sequential.

    The assumption is a “logout” record is always inserted after a “login”. So the logout’s record ID would always be greater than that of the corresponding “login”. A subquery could convert the login information into pairs: ie current and next login record. The corresponding “logout” record id would fall within that range. Once you have matched up login/logouts, use date/time functions to calculate the time elapsed in minutes. Then SUM() the values and format the results however you wish. Note: Obviously proper indexing of the table is a must.

    That said, this method only provides an estimate. It has several flaws:

    • Some databases do not optimize
      subqueries very well. So even with
      indexes, the query performance can
      be quite poor if there is a lot of
      data involved.
    • Relies solely upon the record id
      for identification.
    • Does not account for stray logout
      records or erroneous/duplicates. So
      it may be less accurate than other
      methods
    • Requires more convoluted sql than
      alternative structures

    Anyway, I am not sure of the MySQL syntax off the top of my head. But ignoring the MS Sql Server specific functions, this should be close. The performance in MS SQL was decent with proper indexes, and about 100K records.

    SELECT  SUM(DateDiff(n, li.LogTime, lo.LogTime)) AS TotalTimeInMinutes
    FROM    (
                SELECT  li.RecordID, li.UserID, li.LogType, li.LogTime,
                       (    SELECT MIN(t.RecordID) FROM  YourTable t
                            WHERE t.UserID   = li.UserID
                            AND   t.LogType  = li.logType
                            AND   t.RecordID > li.RecordID
                        ) AS NextRecordID
                FROM   YourTable li
                WHERE  li.UserID = 123
                AND    li.LogType = 'login'
            ) 
            li INNER JOIN YourTable lo ON lo.UserID = li.UserID
                 AND lo.LogType = 'logout'
                 AND lo.RecordID BETWEEN li.RecordID AND li.NextRecordID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like the one below and would like to calculate the
I would like to know if it's possible to select coordinates from a path
Possible Duplicate: Calculate date/time difference in java how would a future date such as
I have two set of samples that are time independent. I would like to
Would be possible to extend this kind of layout so that I can have
I would like to know if it would be possible to replicate the effect
I was wondering if it would be possible to do this: $var = '$something
Would it be possible to extract the following information from logs? Start up/Shut down
Is it possible to subtract time from a calendar? e.g. ..... Calendar DueTime =
Possible Duplicates: Calculating difference in dates in Java How can I calculate a time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.