Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select root node from subtree (PostgreSQL ltree) query, which returns several descendants

Is there a simple way to select the root node of a subtree (PostgreSQL ltree) from a query which returns (potentially) several descendant nodes of that same subtree? I've implemented a rather verbose algorithm for achieving the task (~40 lines, indented and formatted), but it would be awesome if I could leverage the fact that ltree data are in fact trees and have an easily accessible root node. It is important to note that several, distinct subtree roots may be returned from a single query, so I cannot merely sort the data and grab the top result.

June 07, 2012: I have updated the query to my most recent version, which cuts the time complexity in half. It uses a self-anti-join (if you will) to remove all nodes from the subtree which have ancestors in the subtree.

Essentially, my algorithm works as follows:

WITH roots AS
(
  /* Place any query here, which returns a field "ancestry" of type ltree */
)
SELECT roots.*
  FROM roots
WHERE NOT EXISTS
(
  SELECT 1
    FROM roots AS ancestors
  WHERE ancestors.ancestry @> roots.ancestry
    AND ancestors.id <> roots.id
);

(for more details, please see my gist, here: https://gist.github.com/1507368)

like image 539
Dylon Avatar asked Mar 04 '26 02:03

Dylon


1 Answers

Can't you just use the subpath() function?

SELECT
  SUBPATH(ancestry, 0, 1)
FROM
  some_table;
like image 125
Kouber Saparev Avatar answered Mar 05 '26 16:03

Kouber Saparev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!