Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a query using multiple conditions 1

Status
Not open for further replies.

UNCMoochie

Programmer
Feb 1, 2007
11
US
I am trying to create a query where 50% or more of the options in my where clause are true. For example lets say i have 4 cols for a record that relate to some kind of code.
call them Col1, Col2, col3, col4. Instead of writing out all the different permutations of ways 75% or more of the values could be true is there a better way?
currently i have:
where (col1 = 1 and col2 = 1 and col3 = 1 and col4 = 1)
or (col1 = 0 and col2 = 1 and col3 = 1 and col4 = 1)
or (col1 = 1 and col2 = 0 and col3 = 1 and col4 = 1)
or (col1 = 0 and col2 = 0 and col3 = 1 and col4 = 1)

etc... etc... until i get every possible combination.
any help would be greatly appreciated.
 
Thanks for the quick reply Dennis. The columns i'm referring to are columns that represent different flags in a record, so 0 or 1 will work (even though i was jsut using it as an example).

With the query you wrote above, it would also return records where (lets say i am looking for cases where at least 2 of the columns or more have a value of 1) none of the colums have 1.
Basically i'm trying to say that at I need records where at least 2 of the four columns i'm working with have a value of 1. but it could be three of the four columns or all of them.
Another point there are a total of 10 columns that have a flag in it... if it were just 4 i'd write out the different combinations and not bother with trying to find a faster way.
 
I didn't think of that at all. That definitely simplifies things. Thanks Denis. The data currently isn't in 1s and 0s it's actually in letters, but i'm going to convert it for this and change it back afterwards.
Out of curiosity, is there some other way this could work without summing and in cases where you had lets say 3 or 4 values instead of just 2?
thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top