简体   繁体   English

获取上一个和下一个值计数

[英]Get the previous and next value count

I have a table which contains set of events. 我有一个包含事件集的表。 The events can be search, click, open etc. There is a date column associated with each row. 这些事件可以进行搜索,单击,打开等。每一行都有一个日期列。

I'm trying to obtain count of rows where the first event was search and next event was click. 我正在尝试获取第一个事件为搜索而下一个事件为click的行数。 Is there any way to achieve this using lead/lag in redshift ? 有什么办法可以在红移中使用超前/滞后来实现这一目标?

It throws me an error that aggregate functions calls may not have nested function 它引发了一个错误,聚合函数调用可能没有嵌套函数

count(CASE WHEN lead(trim(lower(event)), 1)
                  OVER (
                    ORDER BY timevalue :: TIMESTAMP ASC) = 'click' AND trim(lower(event)) = 'search'
    THEN 1
        ELSE NULL END)              AS my_column,

I would expect your code to work, using a subquery. 我希望您的代码能够使用子查询工作。 I would write it as: 我将其写为:

select count(*)
from (select t.*,
             lead(event) over (order by timevalue) as next_event
      from t
     ) t
where event = 'search' and next_event = 'click';

I would not expect the lower() and trim() to be necessary, but if your data is really dirty, they might be. 我不希望lower()trim()是必需的,但是如果您的数据确实很脏,则可能是这样。

Note: You cannot put a window function as an argument to an aggregation function, so you need to use a subquery. 注意:不能将窗口函数用作聚合函数的参数,因此需要使用子查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM