It's very difficult to describe what I want with words so here are the tables I'm dealing with
Persons
First | Last | ID
John | Smith | 1
Jane | Smith | 2
PerKey
ID | KW | HITS
1 | P.E. | 1
1 | M E | 1
1 | HVAC | 4
2 | Acct | 7
What I want to do is select from persons where criteria meets multiple rows.
For instance I would like to know who has a 'P.E.' AND 'M E' AND 'HVAC'.
In this case that would return John Smith.
I have been able to accomplish this with the IN and HAVING clauses, but that limits me to not being able to filter how many times an individual has that KW and OR clauses.
Say I only wanted someone with 5 hits on 'HVAC', that would rule John Smith out ideally. Here's the query I have atm:
SELECT first, last
FROM persons
LEFT JOIN perkey ON persons.id = perkey.id
WHERE kw IN ('P.E.','M E','HVAC')
GROUP BY first, last, persons.id
HAVING COUNT(DISTINCT kw) = 3
I'm using SQL Server 2008. I cannot change the data structure either as it's proprietary. Thanks for any help/advice.