在子查询中使用雪花中的上限时获取“无法计算不支持的子查询类型”

xcitsw88  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(295)

我有一张雪花桌如下:

create table field_names_to_retrieve("QualifiedApiName" varchar(80));
INSERT INTO field_names_to_retrieve VALUES ('reason');
INSERT INTO field_names_to_retrieve VALUES ('reason__c');
INSERT INTO field_names_to_retrieve VALUES ('name__c'); 
INSERT INTO field_names_to_retrieve VALUES ('email__c');

如果我运行以下查询,它将工作:

SELECT
    "QualifiedApiName",
    CASE WHEN UPPER(REGEXP_REPLACE("QualifiedApiName", '__c$', '')) NOT IN 
            (SELECT "QualifiedApiName" FROM "field_names_to_retrieve")
        THEN REGEXP_REPLACE("QualifiedApiName", '__c$', '')
    ELSE "QualifiedApiName"
END
FROM
"field_names_to_retrieve";

但是,以下查询不起作用。请注意,在子查询的upper中有:

SELECT
    "QualifiedApiName",
    CASE WHEN UPPER(REGEXP_REPLACE("QualifiedApiName", '__c$', '')) NOT IN 
            (SELECT UPPER("QualifiedApiName") FROM "field_names_to_retrieve")
        THEN REGEXP_REPLACE("QualifiedApiName", '__c$', '')
    ELSE "QualifiedApiName"
END
FROM
"field_names_to_retrieve";

你知道为什么带upper的子查询失败了吗?

44u64gxh

44u64gxh1#

很明显,这个表达是一个问题 NOT IN . 我不鼓励使用 NOT IN 因为 NULL 值不是直观地处理的。
它对你有用吗 NOT EXISTS ?

SELECT (CASE WHEN NOT EXISTS (SELECT 1
                              FROM "field_names_to_retrieve" fntr2
                              WHERE UPPER(REGEXP_REPLACE(fntr."QualifiedApiName", '__c$', '')) = UPPER(fntr2."QualifiedApiName")
                             )
            THEN REGEXP_REPLACE("QualifiedApiName", '__c$', '')
            ELSE "QualifiedApiName"
        END)
FROM "field_names_to_retrieve" fntr;

相关问题