gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:33:38 26 4
gpt4 key购买 nike

我在Redshift中有一个基于事件的表。我想将所有事件与系列中的FIRST事件相关联,前提是该事件在该事件之前的N小时内进行。

如果我只关心第一行,那么我会做:

SELECT
event_time
,first_value(event_time)
OVER (ORDER BY event_time rows unbounded preceding) as first_time
FROM
my_table

但是因为我只想将其与过去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

在我 table 上的一点背景。它是用户的操作,因此用户可以有效跳入,执行1-100次操作然后离开。大多数用户每天1-10倍。 session 很少会持续一个小时以上,因此我可以将N设置为1。

如果我只是将PARTITION BY设置为date_trunc('hour',event_time),我将为整个小时内的 session 加倍创建。

假设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

我的目标是获得看起来像
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

最佳答案

到目前为止,答案似乎是“否”。

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

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

当我在Redshift中尝试这种语法时,出现错误“尚不支持范围”

当“还”更改时,有人更新此信息!

关于sql - 我可以在Redshift的窗口函数上添加条件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com