title: How to create temporary table using a common table expression
body: <p>I want to use Temporary table after ;with clause PostgreSQL inside function. I am getting error  <strong>syntax error at or near &quot;create&quot;</strong>.</p>
  <p>this is code</p>
  <pre><code>with counts as(
      select
          
          (   
              select count(cte.&quot;Id&quot;) from cte 
              left join &quot;Medication&quot; m3 on m3.&quot;Id&quot; = cte.&quot;Id&quot;  and m3.&quot;AlfId&quot; = any(_alfids::int[])
              where public.&quot;CheckMedicationStatus&quot;(m3, '{Active}') = true and cte.&quot;ResidentId&quot; = ri.&quot;ResidentId&quot; 
          ) as &quot;ActiveCount&quot;,
          (   
              select count(cte.&quot;Id&quot;) from cte 
              left join &quot;Medication&quot; m3 on m3.&quot;Id&quot; = cte.&quot;Id&quot;  and m3.&quot;AlfId&quot; = any(_alfids::int[])
              where public.&quot;CheckMedicationStatus&quot;(m3, '{Pending}') = true and cte.&quot;ResidentId&quot; = ri.&quot;ResidentId&quot; 
          ) as &quot;PendingCount&quot;
          
      from residentIds ri --where false = true
  )
  Create  TEMPORARY  table (&quot;ActiveCount&quot; int, &quot;PendingCount&quot; int)as 
      select * from counts
  ;
  </code></pre>
  <p>how can I fix it?</p>