且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Amazon Redshift中选择多行填充常量的行?

更新时间:2023-12-04 13:53:12

正确的Postgres语法为:

The correct Postgres syntax would be:

SELECT * FROM (VALUES (1)) AS q (col1);

缺少一组括号.
但是似乎Redshift甚至不支持INSERT之外的VALUES表达式(就像现代的Postgres一样).因此,对于单行:

A set of parentheses was missing.
But it seems Redshift does not even support a VALUES expression outside of INSERT (like modern Postgres does). So, for a single row:

SELECT * FROM (SELECT 1) AS q (col1);

对于多行(不使用要求的UNION ALL),您可以使用临时表.注意(每个文档):

For multiple rows (without using UNION ALL like requested) you can use a temporary table. Note (per documentation):

在会话结束时会自动删除一个临时表 是在其中创建的.

A temporary table is automatically dropped at the end of the session in which it was created.

CREATE TEMP TABLE q(col1 int);
INSERT INTO q(col1)
VALUES (1), (2), (3);

SELECT  * FROM q;

如果可以选择UNION ALL:

SELECT 1 AS col1
UNION ALL SELECT 2
UNION ALL SELECT 3;