在 Redshift 中,如何将 CTAS 与 "if not exists" 子句结合使用?

In Redshift, how do you combine CTAS with the "if not exists" clause?

我在使这个 table 创建查询工作时遇到了一些问题,我想知道我是否 运行 遇到了 redshift 的限制。

这是我想要做的: 我有需要在模式之间移动的数据,并且我需要为动态数据创建目标 tables,但前提是它们尚不存在。

以下是我知道有效的查询:

create table if not exists temp_table (id bigint);

这会创建一个 table(如果它尚不存在),并且工作正常。

create table temp_2 as select * from temp_table where 1=2;

这样就创建了一个空的 table,其结构与前一个相同。这也很好。

但是,当我执行此查询时:

create table if not exists temp_2 as select * from temp_table where 1=2;

Redshift 阻塞并说在附近有一个错误(为了记录,我确实尝试删除 "as" 然后它说在 select 附近有一个错误)

我在 redshift 文档中找不到任何内容,目前我只是在猜测如何解决这个问题。这是我在 redshift 中做不到的事情吗?

我应该提一下,我绝对可以分离出 select 主动创建 table 的查询并用数据填充它,我可能最终会这样做。我只是很好奇是否有人能告诉我该查询有什么问题。

编辑:

我不认为这是重复的。链接到的 post 提供了许多依赖于用户定义函数的解决方案...redshift 不支持 UDF。他们最近确实实现了一个基于 python 的 UDF 系统,但我的理解是它处于测试阶段,而且我们不知道如何实现它。

谢谢你的观看。

I couldn't find anything in the redshift docs, and at this point I'm just guessing as to how to fix this. Is this something I just can't do in redshift?

事实上,CREATE TABLE ... AS SELECTIF NOT EXISTS 的这种组合在 Redshift 中是不可能的(根据 documentation)。关于 PostgreSQL,自 version 9.5.

以来是可能的

关于 SO,这里讨论:PostgreSQL: Create table if not exists AS。接受的答案提供了不需要任何 UDF 或程序代码的选项,因此它们也可能适用于 Redshift。