如何将 CTE 输出插入到 Oracle 中的临时 table?
How to insert CTE output to temp table in Oracle?
在 Oracle 中,我尝试使用以下代码将 CTE 的输出插入临时文件 table,但出现错误。我不想提前创建临时 table,它应该使用 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
只需替换 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;
在 Oracle 中,我尝试使用以下代码将 CTE 的输出插入临时文件 table,但出现错误。我不想提前创建临时 table,它应该使用 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
只需替换 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;