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.
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:
subqueries very well. So even with
indexes, the query performance can
be quite poor if there is a lot of
data involved.
for identification.
records or erroneous/duplicates. So
it may be less accurate than other
methods
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.