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 290547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:00:27+00:00 2026-05-12T06:00:27+00:00

Table1 has a list of items. Table2 has a list of groups the items

  • 0

Table1 has a list of items.
Table2 has a list of groups the items can be associated with.
Table3 is a cross-reference between 1 and 2.

The groups in table 2 are set up in hierarchical fashion.

Key    ParentKey    Name
1      NULL         TopGroup1
2      NULL         TopGroup2
3      1            MiddleGroup1
4      2            MiddleGroup2
5      3            NextGroup1
6      4            NextGroup1
7      2            MiddleGroup3

I want to be able to select from Table1 filtered by Table3.
Select Items from Table1 Where Table3.ParentKey NOT ‘2’ or any of it’s descendants.

From another post here on stackoverflow I’ve been able to use CTE to identify the hierarchy.

WITH Parent AS
(
    SELECT
        table2.Key,
        cast(table2.Key as varchar(128))  AS Path
    FROM
        table2
    WHERE
        table2.ParentKey IS NULL

   UNION ALL

    SELECT
        TH.Key,
        CONVERT(varchar(128), Parent.Path + ',' + CONVERT(varchar(128),TH.Key)) AS Path
    FROM
        table2 TH
    INNER JOIN
        Parent
    ON
        Parent.Key = TH.ParentKey
)
SELECT * FROM Parent

I guess this is really a two part question.

  1. How do you filter the above? For example, return all groups where TopGroup1 isn’t in the lineage.
  2. How would I apply that to filtering results in the cross-referenced table1.
  • 1 1 Answer
  • 1 View
  • 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-12T06:00:28+00:00Added an answer on May 12, 2026 at 6:00 am

    Try this

    -- Table1 (ItemKey as PK, rest of the columns)
    -- Table2 (as you defined with Key as PK)
    -- Table3 (ItemKey  as FK referencing Table1(ItemKey), 
    --         GroupKey as FK referencing Table2(Key))
    
    Declare @Exclude int
    Set @Exclude = 2          
    ;WITH Groups AS     -- returns keys of groups where key is not equal
    (                   -- to @Exclude or any of his descendants
       SELECT t.Key
         FROM table2 t
        WHERE t.ParentKey IS NULL
          and t.Key <> @Exclude
       UNION ALL
       SELECT th.Key,
         FROM table2 th
        INNER JOIN Groups g ON g.Key = th.ParentKey
        Where th.Key <> @Exclude
    )
    SELECT t1.* 
      FROM Table1 t1
     WHERE t1.key in (Select t3.ItemKey 
                        From table3 t3 
                       Inner Join Groups g2 
                          on t3.GroupKey = g2.Key
                     )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list on when items have been handed out. The table has
I have here a list of items in a table. Each item has a
I'm trying to return a simple list of all items in table A (groups)
I have 2 datatables in a dataset. One table has a list called CostTypes.
I have a table articles that has a column company which has list of
I have two tables - table1 and table2 table1 has 14 columns and table2
I have 2 tables as follow : table1 has these fields: year,day,month,name table2 has
I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.
I need to list (and later update) all the items present in the table
I have a list of items in a table and I'd like to create

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.