如何使用通用 table 表达式创建临时 table

How to create temporary table using a common table expression

我想在函数内的 ;with 子句 PostgreSQL 之后使用临时 table。我在“创建”.

处或附近收到错误 语法错误

这是代码

with counts as(
    select
        
        (   
            select count(cte."Id") from cte 
            left join "Medication" m3 on m3."Id" = cte."Id"  and m3."AlfId" = any(_alfids::int[])
            where public."CheckMedicationStatus"(m3, '{Active}') = true and cte."ResidentId" = ri."ResidentId" 
        ) as "ActiveCount",
        (   
            select count(cte."Id") from cte 
            left join "Medication" m3 on m3."Id" = cte."Id"  and m3."AlfId" = any(_alfids::int[])
            where public."CheckMedicationStatus"(m3, '{Pending}') = true and cte."ResidentId" = ri."ResidentId" 
        ) as "PendingCount"
        
    from residentIds ri --where false = true
)
Create  TEMPORARY  table ("ActiveCount" int, "PendingCount" int)as 
    select * from counts
;

我该如何解决?

CREATE TABLE 需要在通用 table 表达式之前

create temp table foo
as
with cte1 as (
  ...
), cte2 as (
 ...
), cte3 as (
 ...
), cte4 as (
 ...
), cte5 as (
 ...
) 
select *
from cte5;