应用滞后函数,直到在红移中条款

发布于 2025-02-12 03:48:51 字数 2760 浏览 0 评论 0原文

以下表(RedShift)

事件URL等级标志
Clicurl11
查看URL22
查看URL33✓clic
url41
查看url52✓

我想添加带有最后一个clic的url列 我尝试使用lag()函数,但我不知道偏移。是否有一种方法可以应用滞后直到上一个clic?

事件URL等级旗帜案例
CLICURL11
查看URL22
查看URL33✓URL1CLICURL4
URL52✓url41
查看
SELECT *,
CASE
   WHEN flag 
        AND event = 'view'
        AND LAG(event) OVER (ORDER BY rank) = 'clic'
        THEN LAG(url)  
END
FROM myTable;

I have the below table (Redshift)

eventurlrankflag
clicurl11
viewurl22
viewurl33
clicurl41
viewurl52

I would like to add a column with the url of the last clic. I tried with LAG() function but I don't know the offset. Is there a way to apply LAG until the previous clic ?

eventurlrankflagCASE
clicurl11
viewurl22
viewurl33url1
clicurl41
viewurl52url4
SELECT *,
CASE
   WHEN flag 
        AND event = 'view'
        AND LAG(event) OVER (ORDER BY rank) = 'clic'
        THEN LAG(url)  
END
FROM myTable;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2025-02-19 03:48:51

这就像一个差距和岛问题一样,您想在每个景观岛的尽头显示最后一个差距(单击)的URL。

我们可以使用点击的累积计数来定义从单击开始的组,然后恢复每个组中唯一单击的URL:

select t.*,
    case when flag and event = 'view' then
        max(case when event = 'click' then url end)
            over(partition by grp order by id)
    end url_click
from (
    select t.*,
        sum(case when event = 'click' then 1 else 0 end) 
            over(order by id) grp 
    from mytable t
) t

请注意,这需要一列来对整个表上的行进行排序 - 我称之为> id

This reads like a gaps-and-islands problem, where you want to show the url of the last gap (a click) at the end of each island of views.

We can use the cumulative count of clicks to define groups that start with a click, and then recover the url of the unique click in each group :

select t.*,
    case when flag and event = 'view' then
        max(case when event = 'click' then url end)
            over(partition by grp order by id)
    end url_click
from (
    select t.*,
        sum(case when event = 'click' then 1 else 0 end) 
            over(order by id) grp 
    from mytable t
) t

Note that this requires a column to sort rows on the whole table - which I called id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文