How to select multiple rows filled with constants in Amazon Redshift?

社会主义新天地 提交于 2020-01-03 08:14:14

问题


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

I need this because for some reason I can't use UNION ALL. Any help will be greatly appreciated.


回答1:


The correct Postgres syntax would be:

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

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

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;

If UNION ALL would be an option:

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


来源:https://stackoverflow.com/questions/28233519/how-to-select-multiple-rows-filled-with-constants-in-amazon-redshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!