Redshift-基于嵌套公用表表达式创建表

时间:2018-07-30 12:15:32

标签: postgresql amazon-redshift create-table

根据“嵌套”公用表表达式(以下示例)返回的结果创建新表的语法是什么?

示例

   WITH allCustomers as (SELECT Customerid FROM Customer_tbl),
    customer_purchasing as (SELECT Customerid, 
                                   Item, 
                                   Price 
                            FROM allCustomers 
                            JOIN purchases_tbl 
                              ON allCustomers.Customerid = purchases_tbl.Customerid
                               ) 
SELECT * FROM customer_purchasing 

问题:如何基于customer_purchasing创建新表 示例中的CTE

1 个答案:

答案 0 :(得分:1)

在写我的问题时找到了答案:通过在示例查询中添加以下语法,可以基于嵌套CTE创建表。

CREATE TEMPORARY TABLE testtable1 as (
   WITH allCustomers as (SELECT Customerid FROM Customer_tbl),
    customer_purchasing as (SELECT Customerid, 
                                   Item, 
                                   Price 
                            FROM allCustomers 
                            JOIN purchases_tbl 
                              ON allCustomers.Customerid = purchases_tbl.Customerid)
SELECT * FROM customer_purchasing)