postgresql - SQL/Postgres: comparing timestamps across rows -


I have a table named trips that looks like this:

  id | Vehicle_id | Start_time | And_time | ---- + ------------ + --------------------- + ---------- ----------- + 1 | 1 | 2014-06-16 22:00:00 | 2014-06-24 03:30:00 | 2 | 1 | 2014-06-24 05:00:00 | 2014-06-28 05:00:00 | 3 | 2 | 2014-06-23 02:00:00 | 2014-06-30 04:00:00 | SQL Fueled: (PG 9.2 only because 9.3 was overloaded on SQL Bedle.)  

start_time and end_time are both timestamps.

I want to search for travels related to the same vehicle_id where a start_time travels the same calendar day or earlier of the journey end_time occurs next calendar day.

As an example, the id will return 1 and above 2 because:

  • 2 's start_time is on the same calendar day ( 2014-06-24 ) as 1 ; And
  • is similar in both rows vehicle_id

It is quite possible that it is not capable of doing SQL related postgrad functions for any Also, advice, suggestions or hints have been given.

The "previous" row can be accessed using values, in this case Interval () function.

  Select id, vehicle_id, start_time, end_time (select id, vehicle_id, start_time, end_time, start_time :: date-lag (end_time :: date) over (time_time division by vehicle_order) trips From diff_to_next as diff_to_prev, end_time :: date - lead (start_time :: date) (by division of vehicle_order by start_time)) where diff_to_prev = 0 or diff_to_next = 0;  

If you also provide a sequence order, then "only" makes sense only from your description it looks like you set the order of the rows id column, but if you can easily change it to use start_date .

The line will not be returned with the id = 1 if the difference was taken only on the previous row and the previous row in the statement.

Expression start_time :: date just gets converted to a timestamp on a date to remove the time-part. It also has an integer value (in days) rather than difference interval .

SQLFiddle:


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -