由于某种原因在Redshift中不起作用的查询

时间:2017-07-27 17:11:45

标签: sql amazon-redshift

假设以下sql表:

CREATE TABLE test_data (
field1 varchar(50),
field2 varchar(50),
counter int NOT NULL DEFAULT 0
)

以及以下数据:

INSERT INTO test_data (field1, field2, counter) VALUES ('10','zyx',10);
INSERT INTO test_data (field1, field2, counter) VALUES ('10','mnb',2);
INSERT INTO test_data (field1, field2, counter) VALUES ('20','abc',15);
INSERT INTO test_data (field1, field2, counter) VALUES ('20','zyx',5);
INSERT INTO test_data (field1, field2, counter) VALUES ('20','mnb',1);

我正在尝试运行以下查询:

 SELECT field1, CASE
     WHEN EXISTS (SELECT 1 from ( SELECT x.field2 AS f
            FROM test_data AS x
            GROUP BY x.field2
            ORDER BY sum(x.counter) DESC
            LIMIT 2) as sub where y.field2 = sub.f) THEN y.field2
     ELSE 'UNKNOWN'
     END AS matched,
     sum(counter) as c
 FROM test_data AS y
 group by field1, matched;

这个查询在MySQL中运行正常,但是我无法让它在Redshift中运行,它会被以下内容炸掉:

ERROR:  This type of correlated subquery pattern is not supported due to internal error

有可行,优雅的工作吗?我认为可以使用临时表来完成,但我正在检查是否有更好的解决方案。

1 个答案:

答案 0 :(得分:2)

一个单独的子查询和一个连接在Redshift中工作,代码更清晰(根据MySQL结果检查并得到相同的结果):

SELECT x.field1, coalesce(y.f,'UNKNOWN'), sum(counter) as c
FROM test_data x
LEFT JOIN (
    SELECT field2 AS f
    FROM test_data
    GROUP BY 1
    ORDER BY sum(counter) DESC
    LIMIT 2
) y
ON x.field2=y.f
GROUP BY 1,2