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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:41:43+00:00 2026-05-11T03:41:43+00:00

I am trying to build an SQL Statement for the following search scenario: I

  • 0

I am trying to build an SQL Statement for the following search scenario:

I have trying to return all of the columns for an individual record for Table A based on the value of the status column in Table B. Each record in table A can have multiple rows in table B, making it a one to many relationship. The status column is nullable with a data type of integer.

Here are the possible values for status in table B:

  • NULL = Pending,
  • 1 = Approved,
  • 2 = Denied,
  • 6 = Forced Approval,
  • 7 = Forced Denial

The end user can search on the following scenarios:

  • Approved – All table B records must have a value of 1 or 6 for status.
  • Denied – One table B record must have a value of 2 or 5. Any other records can have 1,6, or null.
  • Pending – All table B records can have a value of 1,6 or null. One record must be null because it is not considered completed.

UPDATE
I consulted with one of our DBAs and he developed the following solution:

Approved:

SELECT a.* FROM TableA a INNER JOIN TableB ON b.id = a.id WHERE (b.status in (1,6) and b.status IS NOT NULL) AND b.id NOT IN (SELECT id from TableB  WHERE status IS NULL) AND b.id NOT IN (SELECT id from TableB WHERE status in (2,7)) 

Denied:

SELECT a.* FROM TableA a INNER JOIN TableB ON b.id = a.id WHERE (b.status in (2,7)) 

Pending:

SELECT a.* FROM TableA a INNER JOIN TableB ON b.id = a.id WHERE (b.status IN (1,6) OR b.status IS NULL) AND b.id NOT IN (SELECT b.id FROM TableA a INNER JOIN TableB b ON b.id = a.id WHERE (b.status IN (1,6) AND b.status IS NOT NULL) AND b.id NOT IN (SELECT id from TableB WHERE status IS NULL)) AND b.id NOT IN (SELECT id FROM TableB WHERE status IN (2,7)) 

UPDATE 2:
@Micth Wheat – How would I refactor the following solution using the EXIST/NOT EXIST t-sql keyword?

  • 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. 2026-05-11T03:41:44+00:00Added an answer on May 11, 2026 at 3:41 am

    As an example for ‘Approved’:

    select      *  from      A  where     (select count(*) from B where B.parent_id = A.id and B.status in (1,6)) > 0 and (select count(*) from B where B.parent_id = A.id and B.status not in (1,6)) = 0 

    Refactored to use exists and not exists:

    select      *  from      A  where     exists (select * from B where B.parent_id = A.id and B.status in (1,6))  and not exists (select * from B where B.parent_id = A.id and B.status not in (1,6))  

    If you have passed in a criteria, you can package it all up in one query like this, if it is more convenient:

    select      *  from      A  where          (@Criteria = 'Approved' and (select count(*) from B where B.parent_id = A.id and B.status in (1,6)) > 0 and (select count(*) from B where B.parent_id = A.id and B.status not in (1,6)) = 0     ) or  (@Criteria = 'Denied' and (select count(*) from B where B.parent_id = A.id and B.status in (2,7)) > 0     ) or  (@Criteria = 'Pending' and (select count(*) from B where B.parent_id = A.id and B.status not in (2,7)) = 0 and (select count(*) from B where B.parent_id = A.id and B.status is null) > 0     ) 

    Note, I changed the Denied example to be values of 2 and 7, rather than 2 and 5, based on your sample data.

    Edit: You could also use exists and not exists, as Joe suggests.

    Edit: The method using max(case …), often also seen as sum(case …) for counting values, does perform better in some cases (depends mostly on your volume of data whether the performance increase is noticeable – sometimes it can be a big difference). I personally find the subqueries more readable, so I start with them, and if better performance is needed, I would benchmark both methods, and if max(case …) works better, I would switch.

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

Sidebar

Related Questions

I have the following table: I am trying to create an SQL query that
Im trying to improve the following CASE statement to calculate the difference only once.
I have the following four tables: members agent | memberid | firstname | lastname
I am trying to build my skills as an iPhone developer, and presently I
Scenario: I have 3 tables needing to be joined together, a where clause to
Maybe this is a shot in the dark here but I'm trying to find
On OS X. $ java -version java version 1.6.0_20 Java(TM) SE Runtime Environment (build
http://en.wikipedia.org/wiki/Upsert Insert Update stored proc on SQL Server Is there some clever way to
I know that there have been plenty of topics describing this topic but I
SO this is a bit of an odd request but hoping someone on here

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.