I come across this pattern occasionally and I haven’t found a terribly satisfactory way to solve it.
Say I have a employee table and an review table. Each employee can have more than one review. I want to find all the employees who have at least one “good” review but no “bad” reviews.
I haven’t figured out how to make subselects work without knowing the employee ID before hand and I haven’t figured out the right combination of joins to make this happen.
Is there a way to do this WITHOUT stored procedures, functions or bringing the data server side? I’ve gotten it to work with those but I’m sure there’s another way.
Since you haven’t posted your DB Structure, I made some assumptions and simplifications (regarding the
ratingcolumn, which probably is number and not a character field). Adjust accordingly.Solution 1: Using Joins
Solution 2: Grouping By and Filtering with Conditional Count
Both solutions are SQL ANSI compatible, which means both work with any RDBMS flavor that fully support SQL ANSI standards (which is true for most RDBMS).
As pointed out by @onedaywhen, the code will not work in MS Access (have not tested, I’m trusting in his expertise on the subject).
But I have one saying on this (which might make some people upset): I hardly consider MS Access a RDBMS. I have worked with it in the past. Once you move on (Oracle, SQL Server, Firebird, PostGreSQL, MySQL, you name it), you do not ever want to come back. Seriously.