MATCH_RECOGNIZE 与雪花中的 CTE

时间:2021-07-27 12:10:41

标签: sql snowflake-cloud-data-platform common-table-expression sql-view match-recognize

我在具有几个 CTE 的查询中使用 MATCH_RECOGNIZE 函数。当我运行查询时,出现以下错误:

<块引用>

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

在我的查询中,在 MATCH_RECOGNIZE 之前和之后有几个 CTE,部分如下。

WITH cte1 AS (
SELECT *
FROM dataset
WHERE ID IS NOT NULL AND STATUS IS NOT NULL ),

cte2 AS (
SELECT *
FROM cte1
QUALIFY FIRST_VALUE(STATUS) OVER (PARTITION BY ID ORDER BY CREATED_AT) = 'created' )

mr as (
SELECT *
    FROM cte2
    MATCH_RECOGNIZE (
      PARTITION BY ID
      ORDER BY CREATED_AT
      MEASURES MATCH_NUMBER() AS mn,
               MATCH_SEQUENCE_NUMBER AS msn
      ALL ROWS PER MATCH
      PATTERN (c+m+)
      DEFINE
         c AS status='created'
        ,m AS status='missing_info'
        ,p AS status='pending'
    ) m1
    QUALIFY (ROW_NUMBER() OVER(PARTITION BY mn, ID ORDER BY msn) = 1)
          OR(ROW_NUMBER() OVER(PARTITION BY mn, ID ORDER BY msn DESC)=1)
    ORDER BY ID, CREATED_AT ),

cte3 as (
SELECT *
FROM mr
-- some other operations
)

解决这个问题的理想方法是什么?例如创建常规视图、物化视图或临时表等。我尝试创建视图但出现错误,不确定是否也支持。 如何在其他后续 CTE 中使用 MATCH_RECOGNIZE 的结果?

当我添加以下内容时,会出现此错误:

<块引用>

位置 0 处的语法错误行 xx 意外“创建”。

create view filtered_idents AS

SELECT *
FROM cte2
MATCH_RECOGNIZE (

)

1 个答案:

答案 0 :(得分:2)

这似乎是一个未记录在案的限制(我要求我们出色的文档团队解决这个问题)。

与此同时,我可以建议将过程分为几个步骤以使用 match_recognize 结果。

重现错误:

with data as (
    select $1 company, $2 price_date, $3 price
    from values('a',1,10), ('a',2,15)
), cte as (

    select *
    from data match_recognize(
        partition by company
        order by price_date
        measures match_number() as "MATCH_NUMBER"
        all rows per match omit empty matches
        pattern(overavg*)
        define
            overavg as price > avg(price) over (rows between unbounded
                                      preceding and unbounded following)
    )
) 
select * from cte

-- 002362 (0A000): SQL compilation error: MATCH_RECOGNIZE not supported in this context.

两步解决方案:

with data as (
    select $1 company, $2 price_date, $3 price
    from values('a',1,10), ('a',2,15)
)

select *
from data match_recognize(
    partition by company
    order by price_date
    measures match_number() as "MATCH_NUMBER"
    all rows per match omit empty matches
    pattern(overavg*)
    define
        overavg as price > avg(price) over (rows between unbounded
                                  preceding and unbounded following)
)
;


with previous_results as (
    select *
    from table(result_scan(last_query_id()))
)

select * 
from previous_results
;