Avatar of ITMikeK
ITMikeK
Flag for United States of America asked on

Need help with an exclusion query

I am trying to write a query, preferablly in one line that given the records below, yield this result:

a)  Only return BoxNumbers with a Status of 'R',  exlcude any BoxNumbers that have ProcessNumber != 0 AND/OR Status other than 'R'
b)  BoxNumber can only have one record

In the case of the data below, the result should only return record ID 4 (BoxNumber = 2)

ID  BoxNumber  Status  ProcessNumber
1            1                R             0
2             1                               1
3             1                S            0
4            2                R             0
5             3                R            0
6             3                               1

Any help is appreciated.
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
DrewKjell

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
wdosanjos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
DrewKjell

The second criteria you posted has me slightly confused, but I believe the below does what you want, however with your test dataset it will not return any results.  Perhaps I'm misreading the last part of your first criteria.


select *
from <Table>
Where STATUS = 'R'
and ProcessNumber !=0

Or?

select *
from <Table>
Where STATUS = 'R'
or (ProcessNumber !=0 AND STATUS <> 'R')

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes