简体   繁体   English

如何创建一个值列为1到10的表?

[英]How to create a table with a column of values from 1 to 10?

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 我想使用SQL和Redshift创建下表,但不确定如何从头开始创建这种表

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)

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语句,则始终可以对联合值进行内联视图:

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

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);

According to the official documentation from amazon , VALUES list used as constant tables are not supported on redshift. 根据Amazon的官方文档VALUES list used as constant tables 不支持 VALUES list used as constant tables

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM