Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Redshift DATEDIFF?

Im trying to add a date difference calculation to a Redshift SQL query:

SELECT 
   client,
   session,
   person_id,
   min(event_start) as "session_start",
   max(event_finish) as "session_finish",
   sum (case when event_type = 'execute-query-action-1'  then 1 else 0 end) as "Type1 reports run" ,
   sum (case when event_type = 'execute-query-action-2'  then 1 else 0 end) as "Type 2 reports run" ,
   DATEDIFF(MINUTE,session_start,session_finish) as "session_duration_minutes"
FROM usage_log
group by client,person_Id,session

... and I'm getting an error:

function pg_catalog.date_diff("unknown", timestamp with time zone, timestamp with time zone) does not exist HINT: No function matches the given name and argument types. You may need to add explicit type casts.

Any suggestions?

like image 306
user2868835 Avatar asked Oct 18 '25 10:10

user2868835


1 Answers

The DATEDIFF Function is documented as:

DATEDIFF ( datepart, {date|timestamp}, {date|timestamp} )

Thus, it can either take a date or a timestamp.

Your query is passing a timestamp with time zone, hence the error.

Therefore, you could use:

DATEDIFF(MINUTE, session_start::timestamp, session_finish::timestamp)
like image 123
John Rotenstein Avatar answered Oct 20 '25 03:10

John Rotenstein