Search code examples
postgresql

how to combine recursive CTE and normal CTE


I want a first a normal CTE on table and followed by a recursive CTE

how can I combine this two?

I know for multiple pure normal CTE I can do

WITH CTE1 AS(
), CTE2 AS(),...

so I have tried

WITH CTE1 AS(
), RECURSIVE CTE2()

but that gives me a syntax error


Solution

  • Just put the recursive at the start, even if the recursive one comes later:

    with recursive cte1 as (
     ...
    ), cte2 as (
      -- here comes the recursive cte
      ...
    )
    select *
    from ...