Created
May 2, 2024 12:07
-
-
Save ortsed/57e34bd6887f1788a3c316d9e6e49f34 to your computer and use it in GitHub Desktop.
Recursive CTE SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE NetworkCTE AS ( | |
-- Anchor part to select the starting node | |
SELECT source_node, | |
target_node | |
FROM network_connections | |
WHERE source_node = 'Paolo' -- Change this to get someone else's network | |
UNION ALL | |
-- Recursive part to select connected nodes | |
SELECT nc.source_node, | |
nc.target_node | |
FROM network_connections nc | |
JOIN NetworkCTE n ON nc.source_node = n.target_node | |
) | |
SELECT * FROM NetworkCTE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment