RedShift Error when using COUNT (Distinct XXX) ERROR: XX000: This type of associated subquery patter

Posted

技术标签:

【中文标题】RedShift Error when using COUNT (Distinct XXX) ERROR: XX000: This type of associated subquery patterns is not supported due to internal error【英文标题】:RedShift Error when using COUNT (Distinct XXX) ERROR: XX000: This type of correlated subquery pattern is not supported due to internal error 【发布时间】:2019-09-26 12:07:31 【问题描述】:

我有一个通过 Aginity 在 RedShift 中运行的小查询,但出现以下错误:

错误:XX000:由于内部错误,不支持这种类型的关联子查询模式

当前查询:

SELECT 'MYSCHEMA.MYTABLE.FIELDA' as obj_name,
COUNT(*) as rows, 
COUNT(DISTINCT FIELDA) as distinct_vals, 
SUM(CASE WHEN FIELDA in ('00DK','00DC','00DE','00DD','00DB') THEN 1 ELSE 0 END) as enter, 
SUM(CASE WHEN TRIM(FIELDA) IN ('',null) THEN 1 ELSE 0 END) as nulls 
FROM MYSCHEMA.MYTABLE

但是,如果我删除它运行的 DISTINCT:

SELECT 'MYSCHEMA.MYTABLE.FIELDA' as obj_name,
COUNT(*) as rows, 
COUNT(FIELDA) as distinct_vals, 
SUM(CASE WHEN FIELDA in ('00DK','00DC','00DE','00DD','00DB') THEN 1 ELSE 0 END) as enter, 
SUM(CASE WHEN TRIM(FIELDA) IN ('',null) THEN 1 ELSE 0 END) as nulls 
FROM MYSCHEMA.MYTABLE

【问题讨论】:

为 PostgreSQL 标签道歉,我认为它是相关的,因为 Redshift 基于 PostgreSQl ~8.3? MYSCHEMA.MYTABLE 是一个视图吗? @JonScott 是 您需要共享视图的 sql(即更新您的问题),因为这是根本原因 - 实际上您正在将该 sql(在视图中)作为子查询运行。您已经破坏了此处记录的其中一种情况docs.aws.amazon.com/redshift/latest/dg/…。但是,看起来下面的答案无论如何都解决了您的问题。 我已经从答案中得到了它,但我会看看那个文档,谢谢 【参考方案1】:

如果您在子查询中使用窗口函数,它是否有效?

SELECT 'MYSCHEMA.MYTABLE.FIELDA' as obj_name,
       COUNT(*) as rows, 
       SUM( (seqnum = 1)::INT ) as distinct_vals, 
       SUM(CASE WHEN FIELDA in ('00DK','00DC','00DE','00DD','00DB') THEN 1 ELSE 0 END) as enter, 
       SUM(CASE WHEN TRIM(FIELDA) IN ('',null) THEN 1 ELSE 0 END) as nulls 
FROM (SELECT t.*,
             ROW_NUMBER() OVER (PARTITION BY FIELDA ORDER BY FIELDA) as seqnum
      FROM MYSCHEMA.MYTABLE t
     ) t

【讨论】:

以上是关于RedShift Error when using COUNT (Distinct XXX) ERROR: XX000: This type of associated subquery patter的主要内容,如果未能解决你的问题,请参考以下文章

"undefined method `root' for nil:NilClass" error when using "pod install" 解决

Fatal error: Using $this when not in object context in C:AppServwwwABCmodelModel.class.php on l

coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command

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

iOS报错 -pie can only be used when targeting iOS 4.2 or later

When to close cursors using MySQLdb