Untitled

 avatar
unknown
pgsql
a year ago
560 B
5
Indexable
WITH RecursiveCTE AS (
    -- Anchor member
    SELECT 
        ...,       -- columns
        1 AS Depth -- initial depth
    FROM 
        ...
    WHERE 
        ...

    UNION ALL

    -- Recursive member
    SELECT 
        ...,           -- columns
        Depth + 1      -- increment depth
    FROM 
        ...
    JOIN 
        RecursiveCTE 
    ON 
        ...
    WHERE 
        ...
        AND Depth < @MaxDepth  -- termination clause
)

-- Query to use the CTE
SELECT 
    ...
FROM 
    RecursiveCTE
WHERE 
    ...
Editor is loading...
Leave a Comment