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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:05:08+00:00 2026-06-09T12:05:08+00:00

I have a 2 tables: Questions table with Question ID Part Table: Question ID

  • 0

I have a 2 tables:

  Questions table with Question ID

Part Table:

   Question ID
   Part ID
   BAllPartsRequired

The user will select some parts (or may select none) and depending on what was selected certain questions will be displayed.

I want to join the 2 tables for 3 scenerios but do them all in 1 query. I can write each scenerio individually (EDIT I thought I could but scenario 3 I can not get to work where it requires all found in part table to be selected) but can not figure out how to get them all in 1 query (I have done something similar before but cant remember how).

  1. If no parts exist in part table for that question retruen the question

  2. If any part selected exists in part table return question (i.e. user selects 1 part and 5 parts are associated to that question then the question will match and be returned). BAllPartsRequired = false

  3. If user selects parts and ALL of the parts are associated to the question the question is returend but if NOT all parts are selected by user the question is not returend (i.e. user selects 3 parts and there are 4 parts in table then the user will not see the question, but if the user selectes all 4 parts the question will be shown). BAllPartsRequired = true

I am an advanced SQL programmer but this is eluding me and I know I have done this before but how do I get it to work in 1 query, do I do a nested join, a left join, a case on the where statement or something else.

Sample Data:

Question Form Association:          
NFormAssociationID  NQuestionID FormType    
1   1   PAEdit  
2   2   PAEdit  
3   3   PAEdit  
4   4   PAEdit  




Question Part Form Association Table:           
ID  NFormAssociationID  PartNumber  BAllPartsRequired
1   1   1   0
2   2   2   1
3   2   3   1

Query without the new parts table added:

Select ROW_NUMBER() OVER(ORDER BY QL.NOrderBy) AS RowNumber,
QL.NQuestionID, QL.FieldName, QL.Question, QL.BRequired, QFL.FormFieldType, QFL.SingleMultipleSM, 
QFL.CSSStyle
FROM dbo.QuestionFormAssociation QA WITH (NOLOCK)
INNER JOIN dbo.QuestionLookup QL WITH (NOLOCK) ON QA.NQuestionID = QL.NQuestionID
INNER JOIN dbo.QuestionFieldTypeLookup QFL WITH (NOLOCK) ON QL.NFieldTypeID = QFL.NFieldTypeID
WHERE QA.BActive = 1 AND QL.BActive = 1 AND QFL.BActive=1
AND QA.FormType = 'PAEdit'
ORDER BY QL.NOrderBy

Simple query using new table

Select ID
    FROM dbo.QuestionPartFormAssociation 
    WHERE BAllPartsRequired = 1 
    AND PartNumber IN ('1', '2') --'1', '2', '3')   
  • 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-09T12:05:09+00:00Added an answer on June 9, 2026 at 12:05 pm

    It sounds like you are trying to find the eligible questions, based on some criteria.

    In this sitatuion, it is best to summarize first at the question level, and then apply logic to those summaries. Here is an example:

    select q.questionid
    from (select q.questionid,
                 max(case when qp.questionid is null then 1 else 0 end) as HasNoParts,
                 sum(case when qp.partid in (<user parts>) then 1 else 0 end) as NumUserParts,
                 count(qp.questionid) as NumParts,
                 max(qp.AllPartsRequired) as AreAllPartsRequired
          from question q left outer join
               questionpart qp
               on q.questionid = qp.questionid
          group by q.questionid
         ) q
     where HasNoParts = 1 or                                   -- condition 1
           AreAllPartsRequired = 0 and NumUserParts > 0 or     -- condition 2
           AreAllPartsRequired = 1 and NmUserParts = NumParts  -- condition 3
    

    I’ve simplified the table and column names to make the logic clearer.

    updated with full answer from OP:

    Select ROW_NUMBER() OVER(ORDER BY QL.NOrderBy) AS RowNumber,
    QL.NQuestionID, QL.FieldName, QL.Question, QL.BRequired, QFL.FormFieldType, QFL.SingleMultipleSM, 
    QFL.CSSStyle
    FROM dbo.QuestionFormAssociation QA WITH (NOLOCK)
    INNER JOIN dbo.QuestionLookup QL WITH (NOLOCK) ON QA.NQuestionID = QL.NQuestionID
    INNER JOIN dbo.QuestionFieldTypeLookup QFL WITH (NOLOCK) ON QL.NFieldTypeID = QFL.NFieldTypeID
    INNER JOIN (
        select q.NFormAssociationID,
        max(case when qp.NFormAssociationID is null then 1 else 0 end) as HasNoParts,
        sum(case when qp.PartNumber in ('1','2','3') then 1 else 0 end) as NumUserParts,
        count(qp.NFormAssociationID) as NumParts, 
        qp.BAllPartsRequired
        from QuestionFormAssociation q 
        left outer join QuestionPartFormAssociation qp on q.NFormAssociationID = qp.NFormAssociationID      
            AND QP.BActive = 1  
        WHERE Q.FormType = 'PAEdit' 
        AND Q.BActive = 1   
        group by q.NFormAssociationID, qp.BAllPartsRequired
    
    ) QSUB ON QA.NFormAssociationID = QSUB.NFormAssociationID
    WHERE QA.BActive = 1 AND QL.BActive = 1 AND QFL.BActive=1
    AND (
        QSUB.HasNoParts = 1                                                     -- condition 1
        OR (QSUB.BAllPartsRequired = 0 and QSUB.NumUserParts > 0)               -- condition 2
        OR (QSUB.BAllPartsRequired = 1 and QSUB.NumUserParts = QSUB.NumParts)   -- condition 3
    )
    ORDER BY QL.NOrderBy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have four tables user-question contains two columns: questionID, userID, the questions that the
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have a table with questions, where each row is a question and all
I have a question. I have here 2 tables table 1 : products product_id
I have a question regarding the design of two tables. Table 1: The main
I have two tables. User table has user_id and order_num Orders table has order_num,
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
I have 2 mysql tables : Question with the following columns : id, question,
I have the following setup: Table: Question QuestionId Table: QuestionTag QuestionId TagId Table: Tag
I have three types of questions: Vocab (represented by question:answer pairs) Grammar (represented by

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.