Jump to content

Recommended Posts

Hi,

 

I have been trying to do this for days and would appreciate some help!

 

This database was designed by someone else and i have been lumped with writing some reports!

 

I have a table that stores GPS information from vehicles.

I am trying to write a report that will show a vehicles "idle time"

 

Below is the table Structure.

 

Field Type

Date date

Time time

Vehicle int(11)

Latitude char(11)

Longitude char(12)

Street char(50)

Suburb char(50)

Height int(11)

SpeedInst int(11)

SpeedMax int(11)

SpeedAv int(11)

SatCount int(11)

SatsInView int(11)

CourseInst int(11)

CourseAv int(11)

 

As you can imagine, it has ALOT of records

 

So i wrote this query to narrow down the records where the vehicle is idle...

 

SELECT

`vehiclehistory`.`Date`,

`vehiclehistory`.`Time`,

`vehiclehistory`.`Vehicle`,

`vehiclehistory`.`SpeedInst`,

`vehiclehistory`.`Suburb`,

`vehiclehistory`.`Street`,

`vehiclehistory`.`Latitude`,

`vehiclehistory`.`Longitude`,

`vehiclehistory`.`SpeedMax`,

`vehiclehistory`.`SpeedAv`,

`vehiclehistory`.`CourseInst`,

`vehiclehistory`.`CourseAv`

FROM

`vehiclehistory`

WHERE

`vehiclehistory`.`Vehicle` = '1008'

AND

`vehiclehistory`.`Date`= '2007-02-14'

AND

`vehiclehistory`.`SpeedInst` < '1'

AND

`vehiclehistory`.`CourseAv` = `vehiclehistory`.`CourseInst`

GROUP BY

`vehiclehistory`.`CourseAv`

ORDER BY

`vehiclehistory`.`Time` ASC

 

 

FINALLY! What i am trying to do is calculate the time between the records that the query displays... that will give me the idle time...

 

Sorry for the long story but felt i should give you all the information.

 

Thank you!

 

Ray

Link to comment
https://forums.phpfreaks.com/topic/39103-calculate-time-between-records/
Share on other sites

That's a tough one for SQL.  I suggest you fetch all those records you are fetchng and do the analysis in php.  SQL works very well with things like sum, average, count, but it's not suited for things like "Find the difference between adjacent pairs of entries).

 

Well, you could use subqueries if you have approriate indexes to find the "next" entry via a subquery.  But in my experience, that's not a good approach.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.