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 8037379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:54:48+00:00 2026-06-05T02:54:48+00:00

I have a MySQL table containing the points (x/y coordinates) of tracks. Each row

  • 0

I have a MySQL table containing the points (x/y coordinates) of tracks. Each row contains the TrackID, a Timestamp, and the X and Y Positions for that track at that given point in time.

What I want is a list of all TrackIDs that were active during a given time interval (tmin…tmax), sorted by their start-time, even if that start time is outside the interval.

A little illustration might help:

Illustration

As an example: Track 1 is active from t11 till t12, which means I have many rows in my table with ID=1 and with timestamps ranging from t11 to t12.

The desired output would be:

TrackID | StartTime
--------+-----------
    7   |    t71
    1   |    t11
    2   |    t21
    6   |    t61

I tried something like this:

SELECT TrackID, MIN(Timestamp) AS StartTime FROM Tracks WHERE Timestamp BETWEEN tmin AND tmax GROUP BY TrackID ORDER BY StartTime;

However, in the example above I don’t get the real start times for tracks 1 and 7, since all rows with timestamps less than tmin are not considered at all.

Of course I could in a first step just get all active TrackIDs with

SELECT TrackID FROM Tracks WHERE Timestamp BETWEEN tmin AND tmax GROUP BY TrackID;

and then with separate queries find the start times of all these tracks and then sort them in my application code.

But I’m sure there is a way to do this with one SQL query. My table contains millions of rows, so efficiency is an issue here.

  • 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-05T02:54:51+00:00Added an answer on June 5, 2026 at 2:54 am

    One way to think about it is to construct the logic to handle your four special cases in your diagram. These two rules should suffice.

    1. tend > tmin AND
    2. tstart < tmax

    If any of these two conditions are true, then the track should be included. You will need a list of tracks as in your second query with their min and max values, and then perform the comparisons:

    SELECT T.TrackID
      FROM (SELECT TrackID, MIN(Timestamp) AS StartTime, MAX(Timestamp) AS EndTime
            FROM Tracks GROUP BY TrackID) T
     WHERE T.EndTime > tmin AND T.StartTime < tmax
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL MyISAM table containing entries that describe airports. This table contains
I have a MySQL table containing a listing of events that I am trying
I have a MySQL table containing phone numbers that may be formatted in many
My MysQL database contains multiple MyISAM tables, with each table containing millions of rows.
I have a MySQL 5.5 table with a column containing a SET datatype. Each
I have a PHP while loop that generates a table containing input tags. Each
I have a MySql table containing stock quotes (stock_symbol, quote_date, open_price, high_price, low_price, close_price)
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have a table watchlist containing today almost 3Mil records. mysql> select count(*) from
I have mysql table that has a column that stores xml as a string.

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.