繁体   English   中英

在 Snowflake 中的公共表表达式中使用“match_recognize”

[英]Using “match_recognize” in a Common Table Expression in Snowflake

更新:这是回答here

我正在将一个有点复杂的查询放在一起,以使用 Snowflake 中的大型时间序列数据集进行事件检测、连接和基于时间的分箱。 我最近注意到match_recognize可以让我雄辩地检测时间序列事件,但是每当我尝试在公共表表达式( with .. as .. )中使用match_recognize表达式时,我都会收到以下错误:

SQL 编译错误:在此上下文中不支持 MATCH_RECOGNIZE。

我已经做了很多搜索/阅读,但没有发现任何关于 CTE 中match_recognize记录限制。 这是我的查询:

with clean_data as (
    -- Remove duplicate entries
    select distinct id, timestamp, measurement
    from dataset
),

label_events as (
    select *
    from clean_data
        match_recognize (
            partition by id
            order by timestamp
            measures
                match_number() as event_number
            all rows per match
            after match skip past last row
            pattern(any_row row_between_gaps+)
            define
                -- Classify contiguous sections of datapoints with < 20min between adjacent points.
                row_between_gaps as datediff(minute, lag(timestamp), timestamp) < 20
        )
)

-- Do binning with width_bucket/etc. here
select id, timestamp, measurement, event_number
from label_events;

我得到了与上面相同的错误。

这是我没有看到的限制,还是我做错了什么?

非递归 cte 总是可以重写为内联视图:

--select ...
--from (
select id, timestamp, measurement, event_number
from (select distinct id, timestamp, measurement
     from dataset) clean_data
match_recognize (
        partition by id
        order by timestamp
        measures
            match_number() as event_number
        all rows per match
        after match skip past last row
        pattern(any_row row_between_gaps+)
        define
            -- Classify contiguous sections of datapoints with < 20min between adjacent points.
            row_between_gaps as datediff(minute, lag(timestamp), timestamp) < 20
    )mr
-- ) -- if other transformations are required

这并不理想,但至少它允许查询运行。

暂无
暂无

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

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