Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 110455

Using recursion to solve database problems.

6 votes
Accepted

Determine Nodes in Network with PostgreSQL

DELETE FROM example WHERE node = 'f'; INSERT INTO example VALUES ('f', ARRAY['e','f','g'], null), ('g', ARRAY['f','g','h'], null), ('h', ARRAY['g','h'], null); Now applying the math: WITH RECURSIVE … To find all paths of arbitrary lengths (alas the transitive closure), you need a recursive solution, or a while loop. It can't be done with simple SQL. (ie: one query without CTEs.) @ypercube …
McNets's user avatar
  • 23.9k
1 vote

Finding the first parent (manager) of an employee that makes twice as much in a self-referen...

Using a recursive function and LATERAL join Next function returns last indirect manager given a employer id. …
McNets's user avatar
  • 23.9k
5 votes

PostgreSQL - retrieve all IDs in a tree for a given subnode

CREATE FUNCTION get_parent(node_id int) RETURNS integer AS $$ WITH RECURSIVE get_parent AS ( SELECT t1.id, t1.parent_id, t1.name, 0 AS level … WITH RECURSIVE childs AS ( SELECT t1.id, t1.parent_id, t1.name FROM tree t1 WHERE t1.id = get_parent(7) UNION ALL SELECT t2.id, …
McNets's user avatar
  • 23.9k
2 votes
Accepted

Binary tree "get open list" SQL query

In this case I think you don't need a recursive query, simply get those records that do not have two child records. …
McNets's user avatar
  • 23.9k
0 votes

Create parent node and parent id

I've used data supplied on your question. create table foo (Id int, Nodeid int, NodeLevel int, Nodevalue varchar(100)); insert into foo (id, nodeid, nodelevel, nodevalue) values (1, 100, 1, ' …
McNets's user avatar
  • 23.9k
2 votes

Get Nested/Hierarchical Data from SQL Table

IMHO you don't need a recursive solution, you can get it by using a simple JOIN. …
McNets's user avatar
  • 23.9k
3 votes

Joining two ltree based tree tables recursively

Inner join returns all ancestors of an specific path, I've used array_agg(distinc to avoid duplicates. select array_agg(distinct p2.name) permissions from perm …
McNets's user avatar
  • 23.9k