Answers for "SQL Query to Join Two Tables Based Off Closest Timestamp"

1

SQL Query to Join Two Tables Based Off Closest Timestamp

SELECT case.id, case.resolution, case.timestamp, class.value
  FROM closed_cases AS case
  LEFT JOIN (select c.*, 
                    (select min(timestamp)
                     from classifications c1
                      where c1.timestamp > c.timestamp) timeend
             from classifications c) AS class 
  ON case.timestamp >= class.timestamp and 
     (case.timestamp < class.timeend or class.timeend IS NULL)
  WHERE case.timestamp BETWEEN $1 AND $2;
Posted by: Guest on August-03-2021

Python Answers by Framework

Browse Popular Code Answers by Language