【问题标题】:Is it possible to implement conditions in the offset of the lag() function on Snowflake?是否可以在 Snowflake 上的 lag() 函数的偏移量中实现条件?
【发布时间】:2022-11-26 02:36:49
【问题描述】:

我在 Snowflake 上有一张表,其中包含公司管理员和用户之间的消息。这是表格的样子。

id message destination messageable_id sent_at
1 Hello Customer! outgoing 1700103 2022-03-22 22:42:11.000
2 Hello Company! incoming 1700103 2022-03-22 22:39:56.000

我一直在尝试通过使用 lag(sent_at,1) over (partition by messageable_id order by sent_at) 从上一行获取 sent_at 值并计算那里的 datediff 作为响应时间来获取响应时间。

但是,我意识到有些记录我有 3 个连续的传出行,对我来说,获取该组中最早的 sent_at 值比获取最新的值更有意义。

我想知道是否可以在 lag() 语法中对偏移量实施条件。与 IF 3 continue outgoing values in column, then offset = 3 else 1 类似的东西。

到目前为止,我已经研究过使用窗口函数但没有运气。

【问题讨论】:

    标签: sql snowflake-cloud-data-platform


    【解决方案1】:

    这是一种方法:

    select *,lag(sent_at,1) over (partition by messageable_id order by sent_at) 
    from (
       select * , row_number() over (partition by messageable_id , destination order by sent_at asc) rn
       from tablename
    ) t where rn = 1 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多