雪花中不支持的子查询

Posted

技术标签:

【中文标题】雪花中不支持的子查询【英文标题】:Unsupported subquery in snowflake 【发布时间】:2021-03-12 12:59:52 【问题描述】:

我正在尝试在雪花中运行查询:


SELECT DISTINCT RPU.RID,
RA.DATE,
COALESCE(RTH.CT, 0) as RCNT,
COALESCE (R.COMMENT_SITE, PR.COMMENT_SITE) as COMMENT_SITE,

##########################
COALESCE ((SELECT SPD FROM "DB"."SCHEMA"."PRD" WHERE
           COMMENT_SITE = R.COMMENT_SITE AND CLOTH_ID = RPU.GPR
           ORDER BY (IS_ACTIVE = false) LIMIT 1), PR.SPD) as SPD
#########################
FROM "DB"."SCHEMA"."CLOTH_USED" RPU JOIN "DB"."SCHEMA"."CAGR" RA ON RPU.RID = RA.RID 
LEFT OUTER JOIN "DB"."SCHEMA"."PNTR_IMP" PR ON RPU.COMMENT_ID[1] = PR.ID
left outer join "DB"."SCHEMA"."COMMENTS" R ON RPU.COMMENT_ID[1] = R.ID
LEFT OUTER JOIN CLOTH_COUNT RTH ON RTH.CLOTH_ID = RPU.GPR 
ORDER BY RPU.RID DESC, COALESCE(RTH.CT, 0) ASC, RA.DATE DESC LIMIT 1;

我得到的错误是:

SQL 编译错误:无法评估不受支持的子查询类型

以下查询工作正常:

SELECT DISTINCT RPU.RID,
RA.DATE,
COALESCE(RTH.CT, 0) as RCNT,
COALESCE (R.COMMENT_SITE, PR.COMMENT_SITE) as COMMENT_SITE,
FROM "DB"."SCHEMA"."CLOTH_USED" RPU JOIN "DB"."SCHEMA"."CAGR" RA ON RPU.RID = RA.RID 
LEFT OUTER JOIN "DB"."SCHEMA"."PNTR_IMP" PR ON RPU.COMMENT_ID[1] = PR.ID
left outer join "DB"."SCHEMA"."COMMENTS" R ON RPU.COMMENT_ID[1] = R.ID
LEFT OUTER JOIN CLOTH_COUNT RTH ON RTH.CLOTH_ID = RPU.GPR 
ORDER BY RPU.RID DESC, COALESCE(RTH.CT, 0) ASC, RA.DATE DESC LIMIT 1;

SQL 编译错误:无法评估不支持的子查询类型,添加时出现:

COALESCE ((SELECT SPD FROM "DB"."SCHEMA"."PRD" WHERE
           COMMENT_SITE = R.COMMENT_SITE AND CLOTH_ID = RPU.GPR
           ORDER BY (IS_ACTIVE = false) LIMIT 1), PR.SPD) as SPD

如何更正查询?问题在于 where 子句,而没有 where 可以正常工作。如何重写查询?

【问题讨论】:

你能不能用 COLAESCE 函数运行子查询,看看是否有效。 @hkandpal 如果我删除 where 条件,它可以工作,但在 where 条件下它不起作用。 问题是当我对这些值进行硬编码时,它无法引用 R.COMMENT_SITE 和 RPU.GPR 【参考方案1】:

因此,在您的情况下,相关子查询通常可以转换为某种形式的连接,您正在做的主要事情是在两列上匹配并选择随机出现的内容(如果您有超过 1 行处于 active=false 状态)行,这可以转换为 row_number() 并使用 QUALITY 作为 LIMIT 的等价物。

因此:

WITH sub_query AS (
    SELECT 
        spd
        comment_site
        cloth_id
    FROM db.schema.prd
    QUALIFY row_number() OVER (PARTITION BY comment_site, cloth_id ORDER BY IS_ACTIVE = false) = 1
)
SELECT DISTINCT 
    rpu.rid,
    ra.date,
    COALESCE(rth.ct, 0) AS rcnt,
    COALESCE (r.comment_site, pr.comment_site) AS comment_site,

    COALESCE ( sq.spd, pr.spd) as SPD

FROM DB.schema.cloth_used AS rpu 
JOIN db.schema.cagr  AS ra 
    ON rpu.rid = ra.rid 
LEFT OUTER JOIN db.schema.pntr_imp AS pr 
    ON rpu.comment_id[1] = pr.id
LEFT OUTER JOIN db.schema.comments AS r 
    ON rpu.comment_id[1] = r.id
LEFT OUTER JOIN sub_query AS sq
    ON sq.comment_site = r.comment_site AND sq.cloth_id = rpu.gpr
LEFT OUTER JOIN cloth_count AS rth 
    ON RTH.CLOTH_ID = RPU.GPR 
ORDER BY rpu.rid DESC, COALESCE(rth.ct, 0) ASC, ra.date DESC 
LIMIT 1;

应该满足您的需求,现在我使用了 CTE,但如果您愿意,可以将其移至 JOIN 区域,或者像这样需要:

SELECT DISTINCT 
    rpu.rid,
    ra.date,
    COALESCE(rth.ct, 0) AS rcnt,
    COALESCE (r.comment_site, pr.comment_site) AS comment_site,

    COALESCE ( sq.spd, pr.spd) as SPD

FROM DB.schema.cloth_used AS rpu 
JOIN db.schema.cagr  AS ra 
    ON rpu.rid = ra.rid 
LEFT OUTER JOIN db.schema.pntr_imp AS pr 
    ON rpu.comment_id[1] = pr.id
LEFT OUTER JOIN db.schema.comments AS r 
    ON rpu.comment_id[1] = r.id
LEFT OUTER JOIN (
    SELECT 
        spd
        comment_site
        cloth_id
    FROM db.schema.prd
    QUALIFY row_number() OVER (PARTITION BY comment_site, cloth_id ORDER BY IS_ACTIVE = false) = 1
) AS sq
    ON sq.comment_site = r.comment_site AND sq.cloth_id = rpu.gpr
LEFT OUTER JOIN cloth_count AS rth 
    ON RTH.CLOTH_ID = RPU.GPR 
ORDER BY rpu.rid DESC, COALESCE(rth.ct, 0) ASC, ra.date DESC 
LIMIT 1;

【讨论】:

以上是关于雪花中不支持的子查询的主要内容,如果未能解决你的问题,请参考以下文章

雪花:无法评估不受支持的子查询类型

雪花标量 UDF 返回 无法评估不支持的子查询类型

雪花不支持的子查询类型无法在 UDF 标量中评估

以数组为参数的雪花函数因不支持的子查询错误而失败

替换雪花中的子查询

横向压平两列,雪花中不重复