如何在Oracle中将CTE输出插入临时表?

时间:2018-05-04 06:19:30

标签: sql oracle common-table-expression ddl

在Oracle中,我试图使用下面的代码将CTE的输出插入临时表,但是它给出了错误。我不想提前创建临时表,应该使用CTE中的列名和数据类型动态创建。

with cte as (
select ORDER_ID, STATUS_ID, CALL_DATE, SHIP_DATE,
       UPDATE_USER_ID,  UPDATE_TIMESTAMP,
       row_number() over(partition by ORDER_ID order by update_timestamp desc) as rowno 
FROM ORDER_HISTORY
where ORDER_ID in (1001,1002, 1003)
)
create table temp_recent_order as
select * from cte where rowno=1

1 个答案:

答案 0 :(得分:3)

只需替换create table语句的顺序,如下所示:

create table temp_recent_order as
with cte as (
select ORDER_ID,
       STATUS_ID,
       CALL_DATE,
       SHIP_DATE,
       UPDATE_USER_ID,
       UPDATE_TIMESTAMP,
       row_number() over(partition by ORDER_ID order by update_timestamp desc) as rowno
  from ORDER_HISTORY
 where ORDER_ID in (1001, 1002, 1003)
)
select * from cte where rowno=1;