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

  • SEARCH
  • Home
  • 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 7770615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:27:03+00:00 2026-06-01T16:27:03+00:00

I’ve a table in which every x seconds a position is being written to.

  • 0

I’ve a table in which every x seconds a position is being written to. The table consists of the following columns:

lat  | lon  | time  | .....

The ultimate goal is to make the track visible on a map with on the points where the device halted displaying a image. On the points where there are more than one entries over time the rows should be added together to display a row with a ‘timeSpent’ at the location which is the difference of the different rows at the same location. So:

lat  | lon  | timeFirst | timeSpent

For instance this is the ‘raw’ table:

x1,y1, 13:00:12  // Point 1
x1,y2, 13:01:34  // Point 2
x3,y3, 13:02:01  // Point 3
x3,y3, 13:03:03  // Point 3
x3,y3, 13:04:34  // Point 3
x3,y3, 13:05:12  // Point 3
x4,y6, 13:06:23  // Point 4

On point 3 there are multiple entries. Which should be joined together to the following table:

    x1,y1, 13:00:12, null
    x1,y2, 13:01:34, null
    x3,y3, 13:02:01, 13:05:12
    x4,y6, 13:06:23, null

I would really love to have a native MySQL query which makes the result table. But if it’s necessary I could use PHP to parse the source table.

Can anyone help me to get the wanted table?

  • 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-06-01T16:27:05+00:00Added an answer on June 1, 2026 at 4:27 pm

    The problem that it is not clear in your example is that if you add another row to it with values:

    x3,y3, 13:59:59  // Point 3
    

    It will still be Point 3, but you’ll be expecting it to be considered as another point in the sequence. In order to do so you should have a query that would prepare the data like this (notice x3,y3 values):

    +---------+-----+-----+
    | COUNTER | LAT | LON |
    +---------+-----+-----+
    |       1 | x1  | y1  |
    |       2 | x1  | y2  |
    |       3 | x3  | y3  |
    |       3 | x3  | y3  |
    |       3 | x3  | y3  |
    |       3 | x3  | y3  |
    |       4 | x4  | y6  |
    |       5 | x3  | y3  |
    +---------+-----+-----+
    

    Once you have this, you can easily group by. The query that would help you do this is the following:

    select
      if(@lat = lat and @lon = lon, @counter, @counter := @counter + 1) counter,
      @lat := lat lat, @lon := lon lon
    from t, (select @lat := '', @lon := '', @counter := 0) init
    order by time
    

    So, all you need to do with that query is group it by and get the min and max values (and null them if they’re equal). So in order to get the result you’re looking for you should run the following query:

    select
      if(@lat = lat and @lon = lon, @counter, @counter := @counter + 1) counter,
      @lat := lat lat, @lon := lon lon, min(time) timeFirst,
      nullif(max(time), min(time)) timeSpent
    from t, (select @lat := '', @lon := '', @counter := 0) init
    group by counter, lat, lon
    order by time
    

    NOTE: This will also return the counter column. You can get rid of it by wrapping the previous query into another select statement such as:

    select lat, lon, timeFirst, timeSpent from ([previous query]) as FinalQuery
    

    But that will decrease considerably performance. So, my advice would be just not to use that column in your application.

    Result with the counter column removed:

    +-----+-----+-----------+-----------+
    | LAT | LON | TIMEFIRST | TIMESPENT |
    +-----+-----+-----------+-----------+
    | x1  | y1  | 13:00:12  |           |
    | x1  | y2  | 13:01:34  |           |
    | x3  | y3  | 13:02:01  | 13:05:12  |
    | x4  | y6  | 13:06:23  |           |
    | x3  | y3  | 13:59:59  |           |
    +-----+-----+-----------+-----------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.