Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:48:53+00:00 2026-06-13T07:48:53+00:00

I hope the title is clear enough. I’ve been implementing logical AND/OR for tree

  • 0

I hope the title is clear enough. I’ve been implementing logical AND/OR for tree structures which are kept in the database, using a simple nodes and a parent-child association table.
A sample tree has a structure like this:

nodes and their children

A sample tree structure query is as follows:

A contains children of certain types, which in turn contain other nodes

The double lines in the query pattern mean that A has a child of type B (somewhere down its child nodes) OR C. I have implemented A -> HASCHILD -> C -> HASCHILD -> E with an inner join, and A -> HASCHILD -> B -> HASCHILD -> E is implemented like this.
The trick is joining these two branches on A. Since this is an OR operation, either B branch or C branch may not exist. The only method I could think of if to use full outer joins of two branches with A’s node_id as the key. To avoid details, let me give this simplified snippet from my SQL query:

    WITH A as (....),
    B as (....),
    C as (....),
    ......

SELECT * 
      from
          A
          INNER JOIN A_CONTAINS_B ON  A.NODE_ID = A_CONTAINS_B.parent
          INNER JOIN B ON A_CONTAINS_B.children @> ARRAY[B.NODE_ID]
          INNER JOIN .....

          full OUTER JOIN -- THIS IS WHERE TWO As ARE JOINED
          (select
          A2.NODE_ID AS A2_NODE_ID
          from
          A2
          INNER JOIN A_CONTAINS_C ON A2.NODE_ID = C_CONTAINS_C.parent
          INNER JOIN C ON A_CONTAINS_C.children @> ARRAY[C.NODE_ID]
          INNER JOIN ....) 
          as other_branch
          ON other_branch.A2_NODE_ID = A.NODE_ID

This query links two As which actually represent the same A using node_id, and if B or C does not exist, nothing breaks.
The result set has duplicates of course, but I can live with that. I can’t however think of another way to implement OR in this context. ANDs are easy, they are inner joins, but left outer join is the only approach that lets me connect As. UNION ALL with dummy columns for both branches is not an option because I can’t connect As in that case.

Do you have any alternatives to what I’m doing here?

UPDATE

TokenMacGuy’s suggestion gives me a cleaner route than what I have at the moment. I should have remembered UNION.
Using the first approach he has suggested, I can apply a query pattern decomposition, which would be a consistent way of breaking down queries with logical operators. The following is a visual representation of what I’m going to do, just in case it helps someone else visualize the process:

Tree query decomposition

This helps me do a lot of nice things, including creating a nice result set where query pattern components are linked to results.
I’ve deliberately avoided details of tables or other context, because my question is about how to join results of queries. How I handle the hierarchy in DB is a different topic which I’d like to avoid. I’ll add more details into comments. This is basically an EAV table accomponied by a hierarchy table. Just in case someone would like to see it, here is the query I’m running without any simplifications, after following TokenMacGuy’s suggestion:

WITH
    COMPOSITION1 as (select comp1.* from temp_eav_table_global as comp1
                      WHERE
                      comp1.actualrmtypename = 'COMPOSITION'),
    composition_contains_observation as (select * from parent_child_arr_based),
    OBSERVATION as (select obs.* from temp_eav_table_global as obs
                        WHERE
                        obs.actualrmtypename = 'OBSERVATION'),
    observation_cnt_element as (select * from parent_child_arr_based),
    OBS_ELM as (select obs_elm.* from temp_eav_table_global as obs_elm
                        WHERE
                        obs_elm.actualrmtypename= 'ELEMENT'),

     COMPOSITION2 as (select comp_node_tbl2.* from temp_eav_table_global as comp_node_tbl2
                          where
                          comp_node_tbl2.actualrmtypename = 'COMPOSITION'),
    composition_contains_evaluation as (select * from parent_child_arr_based),
    EVALUATION as (select eva_node_tbl.* from temp_eav_table_global as eva_node_tbl
                            where
                            eva_node_tbl.actualrmtypename = 'EVALUATION'),
    eval_contains_element as (select * from parent_child_arr_based),
    ELEMENT as (select el_node_tbl.* from temp_eav_table_global as el_node_tbl
                            where
                            el_node_tbl.actualrmtypename = 'ELEMENT')



select
                      'branch1' as branchid,
                      COMPOSITION1.featuremappingid as comprootid,
                      OBSERVATION.featuremappingid as obs_ftid,
                      OBSERVATION.actualrmtypename as obs_tn,
                      null as ev_ftid,
                      null as ev_tn,
                      OBS_ELM.featuremappingid as obs_elm_fid,
                      OBS_ELm.actualrmtypename as obs_elm_tn,
                      null as ev_el_ftid,
                      null as ev_el_tn

                      from
                      COMPOSITION1
                      INNER JOIN composition_contains_observation ON COMPOSITION1.featuremappingid = composition_contains_observation.parent
                      INNER JOIN OBSERVATION ON composition_contains_observation.children @> ARRAY[OBSERVATION.featuremappingid]
                      INNER JOIN observation_cnt_element on observation_cnt_element.parent = OBSERVATION.featuremappingid
                      INNER JOIN OBS_ELM ON observation_cnt_element.children @> ARRAY[obs_elm.featuremappingid]

UNION

SELECT                  
                        'branch2' as branchid,
                        COMPOSITION2.featuremappingid as comprootid,
                        null as obs_ftid,
                        null as obs_tn,
                        EVALUATION.featuremappingid as ev_ftid,
                        EVALUATION.actualrmtypename as ev_tn,
                        null as obs_elm_fid,
                        null as obs_elm_tn,
                        ELEMENT.featuremappingid as ev_el_ftid,
                        ELEMENT.actualrmtypename as ev_el_tn                       
                  from
                      COMPOSITION2
                      INNER JOIN composition_contains_evaluation ON  COMPOSITION2.featuremappingid = composition_contains_evaluation.parent
                      INNER JOIN EVALUATION ON composition_contains_evaluation.children @> ARRAY[EVALUATION.featuremappingid]
                      INNER JOIN eval_contains_element ON EVALUATION.featuremappingid = eval_contains_element.parent
                      INNER JOIN ELEMENT on eval_contains_element.children @> ARRAY[ELEMENT.featuremappingid]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:48:54+00:00Added an answer on June 13, 2026 at 7:48 am

    the relational equivalent to ∨ is ⋃. You could either use union to combine a JOIN b JOIN e with a JOIN c JOIN e or just use the union of b and c and join on the resulting, combined relation, something like a JOIN (b UNION c) JOIN e

    More completely:

    SELECT *
    FROM a
    JOIN (
        SELECT
            'B' source_relation,
            parent,
            b.child,
            b_thing row_from_b,
            NULL row_from_c
        FROM a_contains_b JOIN b ON a_contains_b.child = b.node_id
    
        UNION
        SELECT
            'C',
            parent
            c.child,
            NULL,
            c_thing
        FROM a_contains_c JOIN c ON a_contains_c.child = c.node_id
    ) a_c ON A.NODE_ID = a_e.parent
    JOIN e ON a_c.child = e.node_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope that title is clear enough. I have 2 projects, MyProject.Website (ASP.NET MVC3
Hopefully the title of my question was clear enough. Here's a really simple version
I hope the title and following text are clear, I'm not very familiar with
I hope the title was good enough to help explain what I am having
I hope I worded the title accurately enough but I typically use Java and
Ok, I'm not sure that my title was clear enough, but I will try
I hope the title of this question is somewhat clear, I couldn't figure out
I hope my title is clear.... What I mean is for exmaple when we
I hope the title is clear. I would like to retrieve the existing google
I hope the title is clear, please read further and I will explain what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.