sqlpostgresqltimestampaggregate-functionsolap-cube

How do you I calculate the difference between two timestamps in PostgreSQL?


I have a SELECT query to retrieve data I need. One of the needed data is a an "aggregate" function to select two timestamp dates and calculate the difference between them. Using several resources and websites, they all gave me save answer and it did not work:

DECLARE @DATE1 TIMESTAMP = actual_route.end_location_time
DECLARE @DATE2 TIMESTAMP = actual_route.actual_trip_start_time
SELECT 
    CONVERT (TIMESTAMPDIFF, @DATE1, @DATE2) AS time_taken, actual_route.time as date_time

I'm getting an error on the "@": syntax error at or near "@" (This is a portion of the code only but the error is here)


Solution

  • You can take the differenc in Postgres:

    select (actual_route.end_location_time - actual_route.actual_trip_start_time) as duration
    

    I'm not sure why you are using SQL Server syntax for this. Nor do I know what timestampdiff is supposed to be.