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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:52:42+00:00 2026-05-23T00:52:42+00:00

I am trying to filter a single table (master) by the values in multiple

  • 0

I am trying to filter a single table (master) by the values in multiple other tables (filter1, filter2, filter3 … filterN) using only joins.

I want the following rules to apply:

(A) If one or more rows exist in a filter table, then include only those rows from the master that match the values in the filter table.

(B) If no rows exist in a filter table, then ignore it and return all the rows from the master table.

(C) This solution should work for N filter tables in combination.

(D) Static SQL using JOIN syntax only, no Dynamic SQL.

I’m really trying to get rid of dynamic SQL wherever possible, and this is one of those places I truly think it’s possible, but just can’t quite figure it out. Note: I have solved this using Dynamic SQL already, and it was fairly easy, but not particularly efficient or elegant.


What I have tried:

  1. Various INNER JOINS between master and filter tables – works for (A) but fails on (B) because the join removes all records from the master (left) side when the filter (right) side has no rows.

  2. LEFT JOINS – Always returns all records from the master (left) side. This fails (A) when some filter tables have records and some do not.


What I really need:

It seems like what I need is to be able to INNER JOIN on each filter table that has 1 or more rows and LEFT JOIN (or not JOIN at all) on each filter table that is empty.

My question: How would I accomplish this without resorting to Dynamic SQL?

  • 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-23T00:52:43+00:00Added an answer on May 23, 2026 at 12:52 am

    In SQL Server 2005+ you could try this:

    WITH
      filter1 AS (
        SELECT DISTINCT
          m.ID,
          HasMatched = CASE WHEN f.ID IS NULL THEN 0 ELSE 1 END,
          AllHasMatched = MAX(CASE WHEN f.ID IS NULL THEN 0 ELSE 1 END) OVER ()
        FROM masterdata m
          LEFT JOIN filtertable1 f ON join_condition
      ),
      filter2 AS (
        SELECT DISTINCT
          m.ID,
          HasMatched = CASE WHEN f.ID IS NULL THEN 0 ELSE 1 END,
          AllHasMatched = MAX(CASE WHEN f.ID IS NULL THEN 0 ELSE 1 END) OVER ()
        FROM masterdata m
          LEFT JOIN filtertable2 f ON join_condition
      ),
      …
    SELECT m.*
    FROM masterdata m
      INNER JOIN filter1 f1 ON m.ID = f1.ID AND f1.HasMatched = f1.AllHasMatched
      INNER JOIN filter2 f2 ON m.ID = f2.ID AND f2.HasMatched = f2.AllHasMatched
      …
    

    My understanding is, filter tables without any matches simply must not affect the resulting set. The output should only consist of those masterdata rows that have matched all the filters where matches have taken place.

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

Sidebar

Related Questions

I'm trying to filter by a list of values using the criteria API. I
I'm trying to filter my shell scripts using maven assembly plugin. Other properties and
I am trying to filter an IEnumerable object of the duplicate values, so I
I am trying to filter items with a stored procedure using like . The
I'm trying to filter some XML in JavaScript using E4X and have some specific
I'm trying to filter a table from an alphabetical <select> input with jQuery. I
I'm trying to filter webserver log files using grep. I need to output all
I'm trying to make a query that looks at a single table to see
I'm trying out the twitter streaming api. I could succesfully filter tweets by using
Im trying to filter rows based on a column say c1 that contains boolean

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.