簡體   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