子查询失败:由于内部错误,不支持此类关联子查询模式;

Posted

技术标签:

【中文标题】子查询失败:由于内部错误,不支持此类关联子查询模式;【英文标题】:Subquery failure: This type of correlated subquery pattern is not supported due to internal error; 【发布时间】:2021-03-28 15:41:50 【问题描述】:

我正在尝试在 Redshift 上运行以下查询,但我不断收到此错误:

亚马逊无效操作:由于内部错误,不支持这种类型的关联子查询模式;

查询如下:

INSERT INTO table_tmp (
 -- straightforward table's schema, fields, datatypes 
 -- the problem is with the SELECT statement that follows
)
SELECT
CHARGEPOINT.Id,
'ABC',
CHARGEPOINT.CurrentStatus,
NULL,
NULL,
CHARGEPOINT.ChargingStationOwner,
CHARGEPOINT.CPOName,
CASE 
    WHEN CHARGEPOINT.CurrentStatus = 'Offline' OR CHARGEPOINT.CurrentStatus = 'Unavailable' OR CHARGEPOINT.CurrentStatus = NULL THEN 'Red'
    WHEN CHARGEPOINT.CurrentStatus = 'Pending' OR CHARGEPOINT.CurrentStatus = 'Unmanaged' THEN 'Amber'
    WHEN CHARGEPOINT.CurrentStatus = 'Online' OR CHARGEPOINT.CurrentStatus = 'Available' THEN 'Green'
END,
NULL,
CAST (CHARGEPOINT.InstallationDate AS timestamp),
NULL,
CHARGEPOINT.FirmwareVersion,
NULL,
CHARGEPOINT.ManufacturerName,
NULL,
CASE
    WHEN CHARGEPOINT.CurrentStatus = 'Online' OR CHARGEPOINT.CurrentStatus = 'Available' THEN TRUE
    ELSE FALSE
END,
NULL,
(SELECT count(CHARGEPOINT_GROUP.Id) FROM x.chargepoint CHARGEPOINT_GROUP GROUP BY CHARGEPOINT.Id),
CHARGEPOINT.IP,
NULL,
CAST (CHARGEPOINT.CreatedAt AS timestamp),
CASE
    WHEN CHARGEPOINT.CurrentStatus = 'Offline' OR CHARGEPOINT.CurrentStatus = 'Unavailable' OR CHARGEPOINT.CurrentStatus = NULL THEN TRUE
    ELSE FALSE
END,
NULL,
NULL,
CHARGEPOINT.Label,
NULL,
CHARGEPOINT.SAPID,
CHARGEPOINT.Serialnumber,
NULL,
NULL,
NULL,
CHARGEPOINT.IMSI,
CHARGEPOINT.ICCID,
CAST (CHARGEPOINT.LastHeartBeat AS timestamp),
'UPDATE',
current_timestamp,
'ABC',
'',
'',
current_timestamp,
current_timestamp,
TRUE

FROM x.chargept CHARGEPOINT;

在对以前的类似问题进行了一些研究之后,我现在怀疑这个子查询是导致这个问题的原因:

(SELECT count(CHARGEPOINT_GROUP.Id) FROM x.chargepoint CHARGEPOINT_GROUP GROUP BY CHARGEPOINT.Id)

所以我尝试使用 WITH 语句并改为这样做:

(WITH cte1 AS (
(SELECT count(CHARGEPOINT_GROUP.Id) 
FROM ext_transform_latest_htb.chargepoint CHARGEPOINT_GROUP 
GROUP BY CHARGEPOINT.Id))
SELECT * from cte1),

但是我得到了和上面一样的错误。

非常感谢任何指导!

【问题讨论】:

(SELECT count(CHARGEPOINT_GROUP.Id) FROM x.chargepoint CHARGEPOINT_GROUP GROUP BY CHARGEPOINT.Id), 不是标量子查询。 什么是chargepointchargept?您可以简化查询并提供更多解释。 【参考方案1】:

是的,这是问题所在,因为它返回的行数超过 1 行。那么如果你把子查询改成这个并给它一个别名呢:

... 
, (
        SELECT
            count(CHARGEPOINT_GROUP.Id)
        FROM
            x.chargepoint CHARGEPOINT_GROUP
        WHERE CHARGEPOINT.Id = CHARGEPOINT_GROUP.chargeptid -- < or whatever FK is
) as ChargePointCount
, ...

【讨论】:

非常感谢您的回答!这说得通。但是,现在我遇到了另一个错误,我不确定如何找到有问题的字段:无效操作:字符类型的值太长详细信息:---------- --------------------------------- 错误:字符类型代码的值太长:8001 上下文:值太长类型字符(40)查询:102626 位置:string.cpp:169 进程:query1_113_102626 [pid=10408] @geosal 好象您在其中一列中插入了 40 多个字符,尝试找出哪一列对应哪个值

以上是关于子查询失败:由于内部错误,不支持此类关联子查询模式;的主要内容,如果未能解决你的问题,请参考以下文章

由于内部错误,不支持相关子查询模式的类型

由于内部错误,不支持相关子查询模式 - 不存在相关子查询

重写相关子查询,Redshift 抛出这种类型的相关子查询模式尚不支持错误

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

Oracle 到 Redshift 查询

使用 case when 时出现 SQL (Redshift) 错误 - 不支持这种类型的相关子查询模式