Cannot use JOIN with generate_series in Redshift

The generate_series function in Redshift works as expected when used in a simple select statement.

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

As soon as I add a JOIN condition, redshift throws

com.amazon.support.exceptions.ErrorException: function generate_series (integer, integer, integer) "not supported"

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 Version

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

Are there any workarounds for this work?

+4
source share
2 answers

You are correct that this does not work in Redshift. See here .

The easiest way is to create a permanent table "manually" in advance with the values ​​in that table, for example. you could have rows in this table for -1000 to +1000, and then select a range from this table,

, -

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;
+2

generate_series Redshift. node.

row_number :

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

+1

Source: https://habr.com/ru/post/1687593/


All Articles