Skip to content
Advertisement

SQL – WITH RECURSIVE doesn’t work, when there is another query before

I have a (postgresql) query, that goes like

This works perfectly fine.

But when I reorder the with queries to that

it suddenly shows a syntax error. This behaviour doesn’t occur, when I have standard non-recursive queries. With non-recursive queries, I can order them, as they please me.

Why does it do that?

Advertisement

Answer

recursive refers to the entire with clause. So just use:

What recursive really means is that Postgres will search for “table” names first as CTEs and then as tables/views in the recursive part of the CTE. The issue is really resolving identifiers. It has no effect on other CTEs.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement