不能在Redshift上使用JOIN和generate_series

时间:2017-10-16 22:25:24

标签: sql amazon-redshift

当在简单的select语句中使用时,Redshift上的

generate_series函数按预期工作。

WITH series AS (
    SELECT n as id from generate_series (-10, 0, 1) n
) SELECT * FROM series;
-- Works fine

一旦我添加了JOIN条件,redshift就会抛出

  

com.amazon.support.exceptions.ErrorException:Function   generate_series(整数,整数,整数)“不支持”

DROP TABLE testing;
CREATE TABLE testing (
  id INT
);
WITH series AS (
  SELECT n as id from generate_series (-10, 0, 1) n
) SELECT * FROM series S JOIN testing T ON S.id = T.id;
-- Function "generate_series(integer,integer,integer)" not supported.

Redshift版本

SELECT version();
-- PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.1485

是否有任何解决方法可以使这项工作?

2 个答案:

答案 0 :(得分:2)

这是正确的,这不适用于Redshift。 请参阅here

最简单的解决方法是创建一个永久表"手动"预先使用该表中的值,例如您可以在该表上拥有-1000到+1000的行,然后从该表中选择范围

因此,对于您的示例,您将拥有类似

的内容
WITH series AS (
  SELECT n as id from (select num as n from newtable where num between -10 and 0) n
) SELECT * FROM series S JOIN testing T ON S.id = T.id;

这对你有用吗?

或者,如果您无法预先创建表格或不愿意创建表格,则可以使用类似

的内容
with ten_numbers as (select 1 as num union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0)
,generted_numbers AS
(
    SELECT (1000*t1.num) + (100*t2.num) + (10*t3.num) + t4.num-5000 as gen_num
    FROM ten_numbers AS t1
      JOIN ten_numbers AS t2 ON 1 = 1
      JOIN ten_numbers AS t3 ON 1 = 1
      JOIN ten_numbers AS t4 ON 1 = 1
)
select  gen_num from generted_numbers
where gen_num between -10 and 0
order by 1;

答案 1 :(得分:2)

Redshift不支持

generate_series。它仅在领导节点上独立工作。

解决方法是对任何有足够行数的表使用row_number

with 
series as (
    select (row_number() over ())-11 from some_table limit 10
) ...

此外,这个问题已被多次询问