Hi all,
I have a problem getting a correct result with my sql code and figured I would see if anybody could help.
I have two fields in a table that have timestamps. I want to find the difference in time between the two stamps and then calculate the average. Here is the code that I have come up with so far. - SELECT AVG(TIMEDIFF(departure_Time, arrival_Time)) as average
-
FROM t1
-
WHERE
-
employee_Number= '3'
I have two stamps: -
departure_Time arrival_Time
-
2007-09-23 13:15:29 2007-09-23 13:42:45
-
2007-09-23 17:26:41 2007-09-23 17:27:41
-
Based on those timestamps and running the code will return '1408' which I believe is 14 minutes and 8 seconds. I believe that the calculations may be correct but I would either like to have a decimal point or just round out the number to 14. Again, this is the average between the two stamps.
Also, if someone has a better way of doing this, I am open to suggestions.
Thanks guys!
Frank
7 20105
Heya, Frank.
The 1408 that you're seeing is 1408 seconds. The average difference is actually 23 minutes and 28 seconds.
Heya, Frank.
The 1408 that you're seeing is 1408 seconds. The average difference is actually 23 minutes and 28 seconds.
Hey Pbmods! I'm glad to see that you are over on this side of the scripts as well. :)
Thanks for the help. Is there a way to convert the seconds to minutes by chance?
Thanks,
Frank
Heya, Frank.
Ermmmmmmmmm... divide by 60?
What you probably meant was how to get the minutes and then the remaining seconds. Fortunately, PHP has a modulus operator.
Heya, Frank.
Ermmmmmmmmm... divide by 60?
What you probably meant was how to get the minutes and then the remaining seconds. Fortunately, PHP has a modulus operator.
LOL.. I'm embarrased. :)
Would you recommend that I do the diff and averages in php rather in mysql?
Thanks Pbmods.
Heya, Frank.
I'd recommend calculating the averages in MySQL simply because then you are fetching fewer rows from the database. The calculations probably take about the same amount of time on either side, but to run the calculations on the PHP side, you have to first fetch every row for that employee.
Now, if you REALLY wanted to speed things up, you could add a `time_diff` column to your table that stores the result of TIMEDIFF(`departure_Time`, `arrival_Time`): -
INSERT
-
INTO
-
`t1`
-
(
-
.
-
.
-
.
-
`departure_Time`,
-
`arrival_Time`,
-
`time_diff`
-
)
-
VALUES
-
(
-
.
-
.
-
.
-
'{$departure_Time}',
-
'{$arrival_Time}',
-
TIMEDIFF('{$departure_Time}', '{$arrival_Time}')
-
)
-
The `time_diff` column (or whatever you decide to call it) would naturally be an int.
Then, as long as the `time_diff` column is indexed, it becomes VERY fast to merely average this column.
Heya, Frank.
I'd recommend calculating the averages in MySQL simply because then you are fetching fewer rows from the database. The calculations probably take about the same amount of time on either side, but to run the calculations on the PHP side, you have to first fetch every row for that employee.
Now, if you REALLY wanted to speed things up, you could add a `time_diff` column to your table that stores the result of TIMEDIFF(`departure_Time`, `arrival_Time`): -
INSERT
-
INTO
-
`t1`
-
(
-
.
-
.
-
.
-
`departure_Time`,
-
`arrival_Time`,
-
`time_diff`
-
)
-
VALUES
-
(
-
.
-
.
-
.
-
'{$departure_Time}',
-
'{$arrival_Time}',
-
TIMEDIFF('{$departure_Time}', '{$arrival_Time}')
-
)
-
The `time_diff` column (or whatever you decide to call it) would naturally be an int.
Then, as long as the `time_diff` column is indexed, it becomes VERY fast to merely average this column.
Your good, Pbmods.. That's a great idea! Thank you so much!!
Frank
Heya, Frank.
Good luck with your project, and if you ever need anything, post back anytime :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lkrubner |
last post by:
This might be an idiot question, but how do you group by timestamps by
date? I mean, given a large number of timestamps, spanning many months,
how do grab them and say how many are from each day?...
|
by: Hans Gruber |
last post by:
Hi all,
I have been struggling with a problem all day, I have been unable to come
up with a working solution.
I want to write a function which takes 2 unix timestamps and calculates
the...
|
by: Bjørn T Johansen |
last post by:
I need to compute the difference of Time fields, in the format HHMM. Is
it possible to do the math in the Select?
Regards,
BTJ
--...
|
by: Robert Schuldenfrei |
last post by:
Dear NG,
I have not heard from anyone about a good book that deals with the
concurrency issue in SQL Server using C#. I have PROMISED Nick I would not
use record locking and I have used an old...
|
by: Craig G |
last post by:
im not too sure how i should be storing the SQL2000 timestamps
basically i return a dataset which is used to populate an editable grid.
this dataset contains the timestamp. how should i be...
|
by: Eric Rechter |
last post by:
Hi,
What's the easiest way in php to calculate the hour and minutes (HH:MM)
between 2 times? (unix timestamps)
thanks in advance!
Eric
|
by: In a little while |
last post by:
thanks
|
by: moshiur |
last post by:
I'm a very new programmer.
I have a sorted file and want to calculate delats between the numbers. That is calculate the difference between first number in line 1 and second number in line 2...
|
by: walt |
last post by:
Hello,
I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |