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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:09:15+00:00 2026-06-04T07:09:15+00:00

I am running a query where I look for a record, and another record

  • 0

I am running a query where I look for a record, and another record a certain amount of time later.

Table definition:

(
  id integer primary key,
  gpsstatus character(2),
  datetime timestamp without time zone,
  lat numeric(9,6),
  lon numeric(9,6),
  alt numeric(9,4),
  time integer,
  datafileid integer,
  shape geometry,
  speed double precision,
  dist double precision,
  shape_utm geometry,
  lokalitet character(128),
  cowid integer
)

There are indexes on datetime,lokalitet,cowid,gpsstatus, gist-index on shape and shape_utm.

The points should be sampled every 5th seconds, so I tried to do

select <something more>,p1.timestamp 
from table p1, table p2 
where p1.timestamp + interval '5 secound' = p2.timestamp

That ran reasonably quick, but then I found out that I had lost quite a few points due to jitter in the sampling, so that the points may be from 4 to 6 seconds apart.

Then I tried:

where    (p2.timestamp, interval'0 second')
overlaps (p1.timestamp + interval '4 second', interval '2 second')

and that took for ages. I also tried the simpler solution:

WHERE p1.timestamp + interval '4 second' <= p2.timestamp
AND   p1.timestamp + interval '6 second' >= p2.timestamp

which also ended up being unusably slow.

The timestamp field has a normal index. Is there a special kind of index are something else that would make this query usable?

The query at the moment:

SELECT
    p1.cowid,
    p1.datetime,
    st_distance(p1.shape_utm, lead(p1.shape_utm)
      OVER (ORDER BY p1.datetime)) AS meters_obs,
    st_distance(p1.shape_utm, lead(p1.shape_utm, 720)
      OVER (ORDER BY p1.datetime)) AS meters_hour,
    observation.observation
  FROM (gpspoint p1 LEFT JOIN observation
                           ON (observation.gpspointid = p1.id)),
       status
  WHERE p1.gpsstatus = status.id
    AND status.use = true;

I may also get an an acceptable query time by asking for some specific intervals.

  • 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-04T07:09:16+00:00Added an answer on June 4, 2026 at 7:09 am

    If you just want the previous record, you can do:

    SELECT  p, LAG(p) OVER (ORDER BY timestamp) AS pp
    FROM    table p
    ORDER BY
            timestamp
    

    If you need a record 4 to 6 seconds before the current, use this:

    SELECT  p1.*, p2.*
    FROM    table p1
    LEFT JOIN
            table p2
    ON      p2.timestamp BETWEEN p1.timestamp - '4 seconds'::INTERVAL
                             AND p1.timestamp - '6 seconds'::INTERVAL
    ORDER BY
            p1.timestamp
    

    This may return multiple previous records if they all are in the range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running a query against a table in a postgresql database. The database is
I'm running a query with retrieves user posts. The table Posts has a column
I am running a query on SQL Server 2005 to look up records within
i am wondering is that possible (best practice) to run two long running query
Running a query which should return all open questions along with the last update
Im running this query on the same server as the web application, so SPQuery.ExpandRecurrence
I'm running a query that is timing out for first two times and returning
I am running a query that returns records from the LEFT of the EXCEPT
I'm running SQL query for MySQL server with ... where name in (term1,term2,term3) I
I'm running this query: SELECT TOP 1 [DVD Copy].[Stock No] FROM [DVD Copy] WHERE

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.