| | |
Meeting at least n criteria from given criteria
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
Try something like this...
MS SQL Syntax (Toggle Plain Text)
SELECT DISTINCT(person) FROM TABLE WHERE ((height = 'tall' AND hair='brown') OR (height='tall' AND eyes='blue') OR (eyes='blue' AND hair = 'brown')
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
you could also use something like this
MS SQL Syntax (Toggle Plain Text)
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
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
Do you know what is wrong??
thanks
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:
sql Syntax (Toggle Plain Text)
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
Last edited by peter_budo; Sep 11th, 2008 at 5:36 am. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
you need to use Sum around the fields you are adding
you also need to use the group by because sum is an aggregate
follow the example and take it from there, your cases are correct, are you sure you need
i'll post your example
MS SQL Syntax (Toggle Plain Text)
Sum(classmatch,countrymatch, launchmatch......etc) AS total
you also need to use the group by because sum is an aggregate
MS SQL Syntax (Toggle Plain Text)
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
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
haven't tested this but this is closer to what you need
MS SQL Syntax (Toggle Plain Text)
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
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
hmmm, try it with the + then, make sure you use sum(a + b + c....)
works for me just using columns
works for me just using columns
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
hmmm, try it with the + then, make sure you use sum(a + b + c....)
works for me just using columns
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)
if you want to try it or try other exercises: http://www.sql-ex.ru/
number 32
thanks for help
![]() |
Similar Threads
- Variable scope problem (C++)
- new to PHP and need help (PHP)
- What is ethical SEO? (Search Engine Optimization)
Other Threads in the MS SQL Forum
- Previous Thread: Installing SQL Server Express on Vista Home Premium
- Next Thread: Upgrade SQL 2005 Standard and SQL 2005 Enterprise Edition
| Thread Tools | Search this Thread |






