SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Variable select query


Variable select query

Author
Message
kennethrbell
kennethrbell
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 28
Hello everybody.

I am a researcher looking for answers.

I have a portfolio of select queries, and am looking for a simple (?) modification.

use table
Go

Select *
from table_name
where
condition 1
condition 2
"
"
""""""""
condition 10

This only returns rows where all 10 conditions are true. Is there a simple way to return all rows where 8 or more of the where statements are true?
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25956 Visits: 17519
Pretty sparse on details here but I think you could do something like this. I'm not so sure about the "simple" part but it should work.


select * from
(
Select *
, case when condition1 then 1 else 0 end
+ case when condition2 then 1 else 0 end
+ case when condition3 then 1 else 0 end as ConditionCount
from table_name
where
condition 1
OR condition 2
) x
where ConditionCount >= 8



This is is a rather strange scenario. :-D

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
batgirl
batgirl
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1705 Visits: 1820
I tested a similar solution successfully...

SELECT *
FROM table
WHERE (
CASE
WHEN condition1 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition2 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition3 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition4 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition5 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition6 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition7 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition8 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition9 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition10 = 'xxx'
THEN 1
ELSE 0
END
) >= 8


kennethrbell
kennethrbell
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 28
Thanks Sean. I'll test it later when I get home. I was trying to use a variable, but I can see how this would work.
kennethrbell
kennethrbell
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 28
Thanks Batgirl.

(I've always wanted to say that!)

I'll check this later.
Bill Talada
Bill Talada
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1149 Visits: 1994
Nice! It seems like a strange request but I get it often. The boss generally wants a few exact match clauses and a subset of several "fuzzy match" clauses to determine what gets combined when I import awful duplicate customer data.

Last week I had a tougher scenario of joining about an average of a dozen rows into one (from thousands) where they matched each other on various subsets of many clauses.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25956 Visits: 17519
kennethrbell (10/18/2013)
Thanks Sean. I'll test it later when I get home. I was trying to use a variable, but I can see how this would work.


You're welcome. Please post back with an update when you get a chance.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1570 Visits: 3317
Bill Talada (10/18/2013)
Nice! It seems like a strange request but I get it often. The boss generally wants a few exact match clauses and a subset of several "fuzzy match" clauses to determine what gets combined when I import awful duplicate customer data.

Last week I had a tougher scenario of joining about an average of a dozen rows into one (from thousands) where they matched each other on various subsets of many clauses.
We do a similar query where there are multiple columns to test a match against and those matches are weighted
case when col1 = val1 then 30 when col1 = other1 then 20 else 0 end +
case when col2 = val2 then 5 else 0 end +
...


However I've found that using this in a where clause can really make the query plan stinkw00t
kennethrbell
kennethrbell
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 28
Thanks everybody. I'd been trying to figure this out for a while. I was using local variables with a case function which apparently don't work in a select statement. I even posed the question to a local SQL users group that meets on Microsoft campus, they thought it was an odd question too. Aah, the life of datamining.

I was able to get Batgirl's query to work. I love the linear regression idea, too. Maybe that will be the next version. I need to figure what values to assign!

Sean, I tried yours but got lost somewhere. Probably because I have several conditions (i.e. where clauses) that I don't want in the count but are universal. I figure if I filter by those first it will speed up the query.

So here is my slight modification of Batgirls query.

SELECT *
FROM table
WHERE
--these UniversalConditions (where clauses) are mandatory all must be true
UniversalCondition1
UniversalCondition2
UniversalCondition3
UniversalCondition4
--inside the Case Statement are Variable Conditions where I will accept 8
--out of 10
(
CASE
WHEN condition1 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition2 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition3 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition4 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition5 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition6 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition7 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition8 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition9 = 'xxx'
THEN 1
ELSE 0
END + CASE
WHEN condition10 = 'xxx'
THEN 1
ELSE 0
END
) >= 8


Is there always one more question?

Mine is, "is there a way to have the total count display as well?" In other words, is this row a "10" a "9" or an "8"?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search