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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:28:07+00:00 2026-05-23T05:28:07+00:00

This query is a subset of a large query, where I’m OUTER APPLY ‘ing

  • 0

This query is a subset of a large query, where I’m OUTER APPLY‘ing a bunch of values, to filter out results later

I’ve got some data:

Table: Items
        ID  |   Material   |  Form
    ----------------------------------
        1   |   Aluminium  |  Sheets
       ------------------------------
        1   | Carbon Steel |  Bars
       ------------------------------
        2   |   Aluminium  |  Bars

I want to find the matching IDs, that satisfy a given input. The input can be in one of three forms, and can have one or many rows. When an input has multiple rows, the item must satisfy ALL rows. Examples of input are given below:

@Input type 1: (just a material, one or multiple allowed)    
    Material     |  Form
    -------------------
    Aluminium    |  NULL

@Input type 2: (material and a form, one or multiple allowed)   
    Material     |  Form
    -------------------
    Aluminium    |  Sheets

@Input type 3: (one or more material and form, with one or more materials)  
    Material     |  Form
    -------------------
    Aluminium    |  Sheets
    Carbon Steel |  NULL

I’ve written a query that can handle input type 1 and a query for input type 2, but I need to combine them, and be able to handle input type 3.

Query for Input Type 1:

Select *
From table

OUTER APPLY(
  SELECT top(1) i.Material
  FROM @Input i --Input type 1
  WHERE i.Material NOT IN
    (SELECT items.Material
    FROM Items
    WHERE items.id = table.id)
)MaterailCondition

--this makes sure that there isn't anything selected that does not match Material
WHERE MaterialCondition.Material IS NULL

Query for Input Type 2:

Select *
From table

OUTER APPLY(
  SELECT top(1) i.Material, i.Form
  FROM @Input i --Input type 1
  WHERE i.Material NOT EXISTS
    (SELECT *
    FROM Items
    WHERE items.id = table.id
    AND items.Material = i.Material
    AND items.Form = i.Form)
)MaterailCondition

--this makes sure that there isn't anything selected that does not match Material
WHERE MaterialCondition.Form IS NULL

Again, at this point, I need to be able to

  • Combine the queries into the same outer apply block
  • Accomodate Input Type 3

Any help would be greatly appreciated! Also, if I can explain anything, or be any clearer about any aspect of this, please let me know. I tried to keep it as short and focused as possible.

EDIT

Here would be the desired output from the query

ID  |  Name      |  MaterialCondition.Material
-------------------------------------------
23  | Some Item  |  (any text, such as 'Carbon Steel') <-- This is not a match
12  | Other Item |  NULL                               <-- This IS a match

--(the where clause will filter these out, by saying)
WHERE MaterialCondition.Material IS NULL

So just ID number 12 is returned:

ID  |  Name      |  MaterialCondition.Material
-------------------------------------------
12  | Other Item |  NULL                
  • 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-23T05:28:07+00:00Added an answer on May 23, 2026 at 5:28 am

    So far I’ve gotten to a state that functions, like this:

    Select *
    From table
    OUTER APPLY(  
     SELECT top(1) i.Material  
     FROM @Input i --Input type 1  
     WHERE i.Material NOT IN    
      (SELECT items.Material    
      FROM Items    
      WHERE items.id = table.id)
    )MaterailCondition
    
    OUTER APPLY(  
     SELECT top(1) i.Material, i.Form  
     FROM @Input i --Input type 1  
     WHERE i.Material NOT EXISTS    
      (SELECT *    
      FROM Items    
      WHERE items.id = table.id    
      AND items.Material = i.Material    
      AND items.Form = i.Form)
    )MaterailCondition2
    
    --this makes sure that there isn't anything selected that does not match Material
    WHERE MaterialCondition.Material IS NULL AND MaterialCondition2.Form IS NULL
    

    This will work properly, and I’ve got an outer apply for input type 1 and input type 2, then the outer applys will take care of their respective parts of input type 3. I guess I was just hoping to contain this logic inside of one OUTER APPLY

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

Sidebar

Related Questions

This query works great: var pageObject = (from op in db.ObjectPermissions join pg in
This query is related to this one I asked yesterday. I have a radio
this query will just one row as a result myDataContext db = new myDataContext();
This query gives me syntax error in when-between line. how can i solve them?
This query selects all the unique visitor sessions in a certain date range: select
This query works: item = db.GqlQuery(SELECT * FROM Item WHERE CSIN = 13)[0] although
This query that I have is returning therapists whose 'therapistTable.activated' is equal to false
In this query: SELECT COUNT(*) AS UserCount, Company.* FROM Company LEFT JOIN User ON
Take this query: SELECT * FROM MyTable WHERE MyColumn = 'SomeValue' ORDER BY SomeFakeQualifier.MyColumn
Why this query doesn’t working: SELECT name FROM ( SELECT name FROM table1 UNION

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.