我可以在Redshift的窗口函数中添加条件吗?

Sco*_*ieB 8 sql window-functions amazon-redshift

我在Redshift中有一个基于事件的表.我希望将所有事件与系列中的FIRST事件联系起来,前提是事件发生在此事件之前的N小时内.

如果我所关注的是非常第一行,我想简单地做:

SELECT
   event_time
   ,first_value(event_time) 
      OVER (ORDER BY event_time rows unbounded preceding) as first_time
FROM
   my_table
Run Code Online (Sandbox Code Playgroud)

但是因为我只想把它与过去N小时的第一个事件联系起来,我想要的是:

SELECT
   event_time
   ,first_value(event_time) 
       OVER (ORDER BY event_time rows between [N-hours ago] and current row) as first_time
FROM
   my_table
Run Code Online (Sandbox Code Playgroud)

在我的桌子上的一点背景.它是用户操作,因此用户可以有效地跳转,执行1-100个操作,然后离开.大多数用户每天1-10次.会话很少持续一个多小时,所以我可以设置N = 1.

如果我只是设置一个PARTITION BY date_trunc('hour',event_time),我会为跨越一小时的会话创建双倍.

假设my_table看起来像

id | user_id | event_time
----------------------------------
 1 |   123   | 2015-01-01 01:00:00
 2 |   123   | 2015-01-01 01:15:00
 3 |   123   | 2015-01-01 02:05:00
 4 |   123   | 2015-01-01 13:10:00
 5 |   123   | 2015-01-01 13:20:00
 6 |   123   | 2015-01-01 13:30:00
Run Code Online (Sandbox Code Playgroud)

我的目标是获得一个看起来像的结果

id | parent_id | user_id | event_time
----------------------------------
 1 |   1       |  123    | 2015-01-01 01:00:00
 2 |   1       |  123    | 2015-01-01 01:15:00
 3 |   1       |  123    | 2015-01-01 02:05:00
 4 |   4       |  123    | 2015-01-01 13:10:00
 5 |   4       |  123    | 2015-01-01 13:20:00
 6 |   4       |  123    | 2015-01-01 13:30:00
Run Code Online (Sandbox Code Playgroud)

Sco*_*ieB 6

到目前为止,答案似乎是否定的。

SQL Server 中有一个在框架中使用 RANGE 而不是 ROWS 的功能。这允许查询将值与当前行的值进行比较。

https://www.simple-talk.com/sql/learn-sql-server/window-functions-in-sql-server-part-2-the-frame/

当我在 Redshift 中尝试使用此语法时,出现“尚不支持范围”的错误

当“尚未”发生变化时,有人会更新它!