如何使用 Redshift DATEDIFF?

use*_*835 3 amazon-web-services amazon-redshift

我正在尝试向 Redshift SQL 查询添加日期差异计算:

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
Run Code Online (Sandbox Code Playgroud)

...我收到一个错误:

函数 pg_catalog.date_diff("unknown", timestamp with time zone, timestamp with time zone) 不存在 提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

有什么建议?

Joh*_*ein 6

DateDiff函数被记录为:

DATEDIFF ( datepart, {date|timestamp}, {date|timestamp} )
Run Code Online (Sandbox Code Playgroud)

因此,它可以采用日期时间戳

您的查询正在传递带有 time zone时间戳,因此出现错误。

因此,您可以使用:

DATEDIFF(MINUTE, session_start::timestamp, session_finish::timestamp)
Run Code Online (Sandbox Code Playgroud)