0

I'm trying to do something like:

WITH megalodon_view AS (
     -- 200 lines of gibberish
)
WITH RECURSIVE traverse_table AS (
     -- big queries with multiple uses of megalodon_view for recursive traversing
)

Observe it's not a mather of defining 2 CTEs, but to use on within the scope of the other.

I'd like to use it in production, so I want preferrably not to create anything physically

Whimusical
  • 6,401
  • 11
  • 62
  • 105

2 Answers2

1

You can specify this as:

WITH recursive megalodon_view AS (
     -- 200 lines of gibberish
     ), 
     traverse_table AS (
     -- big queries with multiple uses of megalodon_view for recursive traversing
     )
select . . .;

The with recursive is needed only once, for the first CTE (even though that one is not recursive).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Without exact query it is hard to say what are you trying to achieve. Anyway you could nest WITH like:

WITH RECURSIVE t(n) AS (
     SELECT *
     FROM (WITH cte AS (SELECT 1 )
           SELECT * FROM cte
           ) sub
  UNION ALL
  SELECT n+1 FROM t WHERE n < 100
)
SELECT COUNT(*) FROM t;

DBFiddle Demo

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275