Snowflake SQL错误-函数'-'的无效参数类型:(TIMESTAMP_NTZ(9),TIMESTAMP_NTZ(9))

时间:2019-10-07 19:42:22

标签: sql snowflake-data-warehouse

当我尝试减去时间戳并执行窗口函数(leadlagpartition by)时出现此错误:

  

函数'-'的无效参数类型:(TIMESTAMP_NTZ(9),TIMESTAMP_NTZ(9))

尝试过date_diff,但不能与窗口功能一起使用

SELECT 
   user_id,
   event,
   received_at,
   received_at - LAG( received_at,1) OVER (PARTITION BY user_id ORDER BY received_at) AS last_event
FROM 
   segment_javascript.help_center_opened

2 个答案:

答案 0 :(得分:1)

您不能仅通过减去两个日期来获得数字,就不能采用“ Oracle方式”,必须使用带有单位/小数位数的diff函数,例如:

SELECT 
   ts, 
   TIMESTAMPDIFF(MILLISECONDS, LAG(ts, 1) OVER (ORDER BY ts), ts) tsd
FROM 
   (VALUES (CURRENT_TIMESTAMP), (DATEADD(DAY, 1, CURRENT_TIMESTAMP))) v(ts);

答案 1 :(得分:0)

我想您需要计算与上次事件接收时间相同的user_id的时差。

如果是这样,我认为这会起作用:

SELECT 
   user_id,
   event,
   received_at,
   DATEDIFF(
     MINUTE, -- or any other supported date/time part
     received_at, -- start time 
     LAG( received_at,1) OVER (PARTITION BY user_id ORDER BY received_at) -- end time
     ) AS last_event
FROM 
   segment_javascript.help_center_opened