0

I'd like to create the following table using SQL and Redshift, but I'm unsure how to go about creating this sort of table from scratch

value
1   
2
3   
4   
5   
6   
7   
8   
9   
10  

I've tried the following query based on this question

SELECT ones.n + 10*tens.n + 1000
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),
     (VALUES(0),(1),(2),(3),(4),(5)                ) tens(n)
WHERE ones.n + 10*tens.n + 1000 BETWEEN 0 AND 10

But I get the following error:

syntax error at or near ","
  Position: 52
SELECT ones.n + 10*tens.n + 1000
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),
               ^
     (VALUES(0),(1),(2),(3),(4),(5)                ) tens(n)
Community
  • 1
  • 1
Chris
  • 5,444
  • 16
  • 63
  • 119

3 Answers3

1

I'm not sure what you are ultimately trying to do, but you can always just do an inline view of union'd values if you need a single SELECT statement:

SELECT t.Number
FROM
(
    SELECT 0 AS Number UNION
    SELECT 1 AS Number UNION
    SELECT 2 AS Number UNION
    SELECT 3 AS Number UNION
    SELECT 4 AS Number UNION
    SELECT 5 AS Number UNION
    SELECT 6 AS Number UNION
    SELECT 7 AS Number UNION
    SELECT 8 AS Number
) AS t
jlewis
  • 111
  • 1
  • 4
1

According to the official documentation from amazon, VALUES list used as constant tables are not supported on redshift.

As a workaround, you could either create your own, temporary table and insert the ten numbers, or use something like this:

SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 ...

Hint: Probably you can even generate large number tables, by using the known approach:

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n
FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 ...) ones(n),
     (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 ...) tens(n),
     (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 ...) hundreds(n),
     (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 ...) thousands(n)
ORDER BY 1
slartidan
  • 20,403
  • 15
  • 83
  • 131
0

Why not create a table and then insert the data into it?

Create table t1(value int);
insert into t1 values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
buqing
  • 925
  • 8
  • 25