如何在Amazon Redshift中选择多行填充常量的行? [英] How to select multiple rows filled with constants in Amazon Redshift?
问题描述
我已经尝试了常见的PostgreSQL答案,但似乎不适用于Redshift:
I have already tried the common PostgreSQL answer, but seems like it doesn't work with Redshift:
SELECT * FROM VALUES (1) AS q (col1);
ERROR: 42883: function values(integer) does not exist
我需要这个,因为某些原因我不能使用UNION ALL
.任何帮助将不胜感激.
I need this because for some reason I can't use UNION ALL
. Any help will be greatly appreciated.
推荐答案
正确的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;
这篇关于如何在Amazon Redshift中选择多行填充常量的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!