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

  • Home
  • SEARCH
  • 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 4566528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:48:06+00:00 2026-05-21T18:48:06+00:00

I apologize for the vague title. I couldn’t think how best to summarize the

  • 0

I apologize for the vague title. I couldn’t think how best to summarize the problem. I have a hierarchical table (e.g., ID int, ParentID int) and need to generate a sub-tree for an ID. This is trivially done with a recursive CTE. The difficulty is that, for each node, I need to compute a running bitwise-OR of a set of corresponding values, and then bit-OR that result with the same value for the parent node. This means every node inherits its parent’s bitmask and may set its own additional bits. I can compute this value in the anchor member of the CTE using OUTER APPLY and a technique mentioned in an earlier question I asked. Unfortunately, I can’t compute it the same way in the recursive part of the CTE because it uses SUM and aggregates are not allowed there.

Is there a way to restructure this to do what I want?

declare @ID int
set @ID = 1

;with _Bits_(RowNum, BitMask) as
(
  select
    1,
    1
  union all select
    RowNum + 1,
    BitMask * 2
  from
    _bits_
  where
    RowNum < 31
),
_Tree_ as
(
  select
    a.ID,
    a.ParentID,
    b.BitMask
  from
    Tree a
    outer apply
    (
      select
        sum(distinct y.BitMask) as BitMask
      from
        BitValues x
        inner join _Bits_ y
          on (x.Value & y.BitMask) <> 0
      where
        x.ID = a.ID
    ) b
  where
    a.ID = @ID
  union all select
    a.ID,
    a.ParentID,
    c.BitMask | b.BitMask
  from
    Tree a
    inner join _Tree_ b
      on b.ID = a.ParentID
    outer apply
    (
      select
        sum(distinct y.BitMask) as BitMask
      from
        BitValues x
        inner join _Bits_ y
          on (x.Value & y.BitMask) <> 0
      where
        x.ID = a.ID
    ) c
)
select * from _Tree_

EDIT

If it helps to conceptualize the problem: the hierarchy is much like a directory structure, and the bitmasks are like permissions that are inherited from parent folders.

Example data

create table Tree (ID int primary key, ParentID int null foreign key references Tree (ID))

insert Tree values (1, null)
insert Tree values (2, 1)
insert Tree values (3, 1)

create table BitValues (ID int not null foreign key references Tree (ID), BitMask int not null)

insert BitValues values (1, 1)
insert BitValues values (2, 2)
insert BitValues values (2, 4)
insert BitValues values (3, 8)
insert BitValues values (3, 16)
insert BitValues values (3, 32)

For @ID 1, I would expect the query to return:

+----+----------+---------+
| ID | ParentID | BitMask |
+----+----------+---------+
|  1 |   NULL   |       1 |
|  2 |        1 |       7 |
|  3 |        1 |      57 |
+----+----------+---------+
  • 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-05-21T18:48:07+00:00Added an answer on May 21, 2026 at 6:48 pm
    declare @ID int;
    set @ID = 1;
    
    with extrarows as
    (
       select t.id, null as parent, v.BitMask as total
       from tree t
       join BitValues v on t.id = v.id
       where t.id = @ID
    
       union all 
    
       select t.id, r.id, v.BitMask | r.total
       from extrarows r
       join Tree t on r.id = t.parentid
       join BitValues v on t.id = v.id
    )
    select id, parent, 
      MAX(total & 1) +
      MAX(total & 2) +
      MAX(total & 4) +
      MAX(total & 8) +
      MAX(total & 16) +
      MAX(total & 32) +
      MAX(total & 128) +
      MAX(total & 256) +
      MAX(total & 512) +
      MAX(total & 1024) +
      MAX(total & 2048)  -- more if you want em.
         as BitMask 
    from extrarows   
    group by id, parent
    

    Some notes:

    • I make an assumption the incoming @id is the “root” of the tree. (Feel free to crawl up the tree to find the starting bit mask of the root if this does not meet your needs.)

    • While summing MAX of the bits does work it might not be performant for large bit strings over many records. I don’t know how many bits you have but it is less then 16 or so it should be fine — like to hear about your findings.

    • To improve performance switch to a custom C# aggregate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize for vague title so let me explain. Suppose you have a page
I apologize for the Title of this Question, but I couldn't think of a
I apologize for the vague title, but I can't think of a better way
Apologies for vague title as i couldn't think what the name is. Basically creating
I apologize it the title is unclear, but I couldn't think of any other
First, I wish to apologize if the title is vague, the question is hard
Apologies for the vague title, but this is quite a difficult problem to explain
Apologies for the vague question. Here it is: I have a table object created
I apologize for the terrible title...it can be hard to try to summarize an
I apologize for the vague title. I am attempting to write a query that

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.