【问题标题】:Nested window function not working in snowflake嵌套窗口函数在雪花中不起作用
【发布时间】:2020-04-20 23:42:05
【问题描述】:

我正在将 spark sql 迁移到 snowsql。 有一次我遇到了一个场景,我在 spark sql 中使用了嵌套窗口函数。我想将该sql查询迁移到雪花中。但是雪花不支持嵌套窗口函数。

Spark sql 查询 -

SELECT 
    *,
    (case when (
        (
            lead(timestamp -lag(timestamp)
                over (partition by session_id order by timestamp))
                over (partition by session_id order by timestamp)
        ) is not null)
     then  
    (
        lead(timestamp -lag(timestamp)
            over (partition by session_id order by timestamp))
            over (partition by session_id order by timestamp)
    ) 
    else 0 end)/1000 as pg_to_pg
FROM dwell_time_step2

输出 - 我已尝试将上述查询转换为雪花,如下所示。

转换后的 Snowsql -

with lagsession as (
SELECT 
    a.*,
    lag(timestamp) over (partition BY session_id   order by timestamp asc) lagsession 
FROM mktg_web_wi.dwell_time_step2 a
)

select 
    a.,
    nvl(lead(a.timestamp - b.lagsession) over (partition BY a.session_id order by   a.timestamp),0)/1000 pg_to_pg
FROM mktg_web_wi.dwell_time_step2 a,
    lagsession b
WHERE a.key=b.key
order by timestamp;

输出 -

这里,问题出在 Snow-sql 输出中。 Dwelltime 值被分配给不同的 url。

期望 spark-sql 查询在 snowsql 上工作,并且两种情况下的输出应该相同。

如果有人知道如何解决这个问题,请告诉我。

谢谢!!

【问题讨论】:

    标签: snowflake-cloud-data-platform snowflake-schema snowsql


    【解决方案1】:

    我认为将它从嵌套窗口函数更改为 cte 已经改变了滞后和领先所指的记录,但这很难让我理解。

    无论如何,如果我在这里理解您的代码,我认为有一种更简单的方法,只有一个 windows 函数。

    select 
        a.*,
        (nvl(lead(a.timestamp) over (partition BY a.session_id order by a.timestamp) - a.timestamp)/1000,0) pg_to_pg
    FROM mktg_web_wi.dwell_time_step2 a
    order by timestamp;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 2021-03-18
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 2022-07-06
      相关资源
      最近更新 更多