Per your update:
The timestamp is in <yyyymmddhhmmss>format where the hour is in the 24 hour clock format
The average time formula should be
(The total time spent by the customers in the system)/(Total number of customers)
A few thoughts (more SQL-driven than Perl):
If your data isn't a date column, but holds a date value, you can reformat it, and use the MySQL UNIX_TIMESTAMP to get the number of seconds since 1/1/1970.
The reason that's useful is that if you convert your 'in' date and 'out' date and *then* subtract, you should be left with the total number of seconds between them.
Doing this, you can gain a huge time benefit by accepting that the mathematical order of precedence for addition and subtraction are equivalent, and you can get total seconds spent in the system by: (Total seconds of the converted in dates) minus (Total seconds of the out dates)
So:
A: Select SUM(UNIX_TIMESTAMP($date)) where record_type = 'I';
B: Select SUM(UNIX_TIMESTAMP($date)) where record_type = 'O';
C: Select COUNT(*)) where record_type = 'I';
Then, (A - B) / C gives you the value you're looking for, in seconds, right?
Btw, you could easily combine A&C into one query, without making the query too hard to read... I left them separate to make it simple to read.
If you're worried about the SUM columns getting too large, then create a VIEW on the table which combines the two rows into one, then do your initial SUM on the subtraction of wach row of the view... Should get the same thing
Does that help? I know it's not a Perl answer, but it didn't really seem like a Perl question, either.
Trek
|