You are right, Redshift does not currently support generate_series. One way around this is to create your own series table and join it. In my example below, I simply did row_number () against the pg_attribute table to generate a sequence. You can adjust the value of TOP (v) to adjust now the many numbers you want in your sequence, if you need more than pg_attribute can give you, try cross-connecting pg_attribute with itself. I am not saying that this is the best way to generate a sequence table; you can generate it in any way; my main point is that you will need to replace generate_series.
Once you have the show table, then this is a simple join to get the result. Full example:
-- Setup Example CREATE TABLE test ( col1 char(2), col2 char(2), col3 char(2), col4 integer ); INSERT INTO test(col1, col2, col3, col4) VALUES ('A1', 'A2', 'A3', 4), ('B1', 'B2', 'B3', 3), ('C1', 'C2', 'C3', 1); -- Generate 10 sequence numbers to table. Adjust as needed or roll your own SELECT TOP 10 ROW_NUMBER() OVER (PARTITION BY attnum ORDER BY attnum) n INTO sequence FROM pg_catalog.pg_attribute; -- Example Query SELECT col1, col2, col3, sn FROM test t INNER JOIN sequence s ON sn <= t.col4 ORDER BY col1, col2, col3, sn; -- Clean up DROP TABLE sequence; DROP TABLE test;
source share