By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.

You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

Here is some SQL code to compare nodes and structures of trees in the Nested Sets model for hierarchies. Rudy Limeback replied to a question about comparing rows in different tables on 21 September 2001. The requester was asking it in regards to my nested sets model of trees, so I would like to reply.

There are really several kinds of equality comparisons when you are dealing with a hierarchy:

Same nodes in both tables

Same structure in both tables

Same nodes and structure in both tables -- they are identical

Let me once more invoke my semi-quasi-famous organization chart in the nested sets model. If you do not understand the nested sets model, then read the earlier articles here and here:

Rudy gave a few different ways of matching nodes between Personnel and Personnel_2. Here is one way that is a little different:

SELECT 'They have the same nodes'
FROM Personnel_2 AS P0
FULL OUTER JOIN
Personnel_3 AS P1
ON P0.emp = P1.emp
HAVING (SELECT COUNT(*) FROM Personnel_2)
= (SELECT COUNT(*) FROM Personnel_3)
AND (SELECT COUNT(*) FROM Personnel_2)
= COUNT(*)
AND (SELECT COUNT(*) FROM Personnel_3)
= COUNT(*);

In fairness, a FULL OUTER JOIN is hard to find in many SQL products, even today. The idea is that anyone without a match in the other table will get generated NULLs and become a row by himself for the count.

Case two: Same structure

I will not bother with an ASCII drawing, but let's present a table with sample data that has different people inside the same structure.

If these things are really separate trees, then life is easy. You do the same join as before but with the (lft,rgt) pairs:

SELECT DISTINCT 'They have the same structure'
FROM Personnel AS P0
WHERE COUNT(P0.emp)
= (SELECT COUNT(*)
FROM Personnel AS P0
FULL OUTER JOIN
Personnel_3 AS P1
ON P0.lft = P1.lft
AND P0.rgt = P1.rgt);

Case three: Same nodes and same structure

This is not a big jump to simply extend the predicate:

SELECT DISTINCT 'They are identical'
FROM Personnel AS P0
WHERE COUNT(P0.emp)
= (SELECT COUNT(*)
FROM Personnel AS P0
FULL OUTER JOIN
Personnel_2 AS P1
ON P0.lft = P1.lft
AND P0.rgt = P1.rgt
AND P0.emp = P1.emp);

But more often than not, you will be comparing subtrees within the same tree. This is best handled by putting the two sub-trees into a canonical form. First you need the root node (i.e the emp in this sample data) and then you can re-number the (lft, rgt) pairs with a derived table of this form:

The derived table with have its (lft,rgt) pairs start at zero instead of one. That is actually an advantage when you insert a canonical tree structure into an existing table, but that is another article.

If you would like to see this all in one monster query:

SELECT CASE COUNT(P.emp)
WHEN (SELECT COUNT(*)
FROM Personnel AS P0
FULL OUTER JOIN
Personnel_3 AS P1
ON P0.lft = P1.lft
AND P0.rgt = P1.rgt
AND P0.emp = P1.emp)
THEN 'The trees are identical'
WHEN (SELECT COUNT(*)
FROM Personnel AS P0
FULL OUTER JOIN
Personnel_2 AS P1
ON P0.emp = P1.emp)
THEN 'The trees have the same nodes'
WHEN (SELECT COUNT(*)
FROM Personnel AS P0
FULL OUTER JOIN
Personnel_2 AS P1
ON P0.lft = P1.lft
AND P0.rgt = P1.rgt)
THEN 'The trees have the same structure'
ELSE 'The trees have different node sets and structure'
FROM Personnel AS P;

This depends on the CASE expression operating from left to right and returning the first TRUE clause it finds. Since there will a lot of indexes on the tree tables, joins and aggregate functions should be relatively fast to perform. For an exercise, try writing the same structure queries with the adjacency list model.

0 comments

Register

Login

Forgot your password?

Your password has been sent to:

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy