954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Meeting at least n criteria from given criteria

Hi I wonder how to write a query where given record meets at list n criteria from given criteria?
For example i have a table PERSON like this:

Name
Height (short, medium, tall)
HairColor (brown, blonde, black)
EyesColor (blue, green, brown)

I want to fine people who meet at least two criteria from below:

blue eyes
brown hair
tall

How to do that??

Thanks

coolick81
Newbie Poster
5 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Try something like this...

SELECT distinct(person) from table where ((height = 'tall' and hair='brown') or (height='tall' and eyes='blue') or (eyes='blue' and hair = 'brown')
coderboi
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Ok it is right but what if i have 10 criteria and want a person to met at least 3 of them?
U can't just do it the same way as they are too many combinations....

coolick81
Newbie Poster
5 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

you could also use something like this

select person_id, hairmatch, colormatch, eyematch, sum(hairmatch, colormatch, eyematch) as total from
(select person_id, 
   (case when Hair = 'Tall' then 1 else 0 end) as hairmatch,
(case when HairColor = 'brown' then 1 else 0 end) as colormatch,
(case when EyeColor = 'blue' then 1 else 0 end) as eyematch
from
person)
group by person_id, hairmatch,colormatch,eyematch

-- add this to filter totals
where total >= 2
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

Hi, thank you for the hint.
I used it in my real example but still making some syntax errors:
the tables are:
Classes(class, type, country, numGuns, bore, displacement)
Ships(name, class, launched)

I just need to get a name of a ship meeting at leat 4 criteria....

my query looks like:

select name from (select name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch,  classmatch + countrymatch + launchedmatch + typematch + displacementmatch + borematch + numgunsmatch as total,
(case when class = 'Kongo' then 1 else 0 end) as classmatch,
(case when country = 'USA' then 1 else 0 end) as countrymatch,
(case when launched = '1915' then 1 else 0 end) as launchedmatch,
(case when type = 'bb' then 1 else 0 end) as typematch,
(case when bore = '15' then 1 else 0 end) as borematch,
(case when numguns = '8' then 1 else 0 end) as numgunsmatch,
(case when displacement = '32000' then 1 else 0 end) as displacementmatch
from ships, classes where ships.name=classes.class and total>=4)


Do you know what is wrong??

thanks

coolick81
Newbie Poster
5 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

you need to use Sum around the fields you are adding

Sum(classmatch,countrymatch, launchmatch......etc) as total


you also need to use the group by because sum is an aggregate

group by
name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch


follow the example and take it from there, your cases are correct, are you sure you need

i'll post your example

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

haven't tested this but this is closer to what you need

select name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch, 
Sum(classmatch ,countrymatch , launchedmatch , typematch , displacementmatch , borematch , numgunsmatch) as total
 from
-- subquery
(select name,
	(case when class = 'Kongo' then 1 else 0 end) as classmatch,
	(case when country = 'USA' then 1 else 0 end) as countrymatch,
	(case when launched = '1915' then 1 else 0 end) as launchedmatch,
	(case when type = 'bb' then 1 else 0 end) as typematch,
	(case when bore = '15' then 1 else 0 end) as borematch,
	(case when numguns = '8' then 1 else 0 end) as numgunsmatch,
	(case when displacement = '32000' then 1 else 0 end) as displacementmatch
from ships
-- you need to do your joins on the tables here to pull class, country ... etc) 
group by name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch
where total>=4
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

hmm it tells me: The Sum function requires 1 argument(s). ?
Maybe you have to use '+' ??

coolick81
Newbie Poster
5 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

hmmm, try it with the + then, make sure you use sum(a + b + c....)

works for me just using columns

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

hmmm, try it with the + then, make sure you use sum(a + b + c....)

works for me just using columns

ok, i got this: select name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch,
Sum(classmatch + countrymatch + launchedmatch + typematch + displacementmatch + borematch + numgunsmatch) as total
from
(select name,
(case when class = 'Kongo' then 1 else 0 end) as classmatch,
(case when country = 'USA' then 1 else 0 end) as countrymatch,
(case when launched = '1915' then 1 else 0 end) as launchedmatch,
(case when type = 'bb' then 1 else 0 end) as typematch,
(case when bore = '15' then 1 else 0 end) as borematch,
(case when numguns = '8' then 1 else 0 end) as numgunsmatch,
(case when displacement = '32000' then 1 else 0 end) as displacementmatch
from ships where classes.class=ships.class and total>=4
group by name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch)

still i get "Incorrect syntax near ')' "
if you want to try it or try other exercises: http://www.sql-ex.ru/
number 32

thanks for help

coolick81
Newbie Poster
5 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

once again you need to group by using the outer query

from ships where classes.class=ships.class)
-- note the end parenthese
-- now put your group by and total
group by name, classmatch, borematch, displacementmatch, countrymatch, launchedmatch, typematch, numgunsmatch
where total>=4
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You