At least one X but no Ys Query

前端 未结 4 999
梦如初夏
梦如初夏 2021-01-06 11:23

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

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 11:40

    Since you haven't posted your DB Structure, I made some assumptions and simplifications (regarding the rating column, which probably is number and not a character field). Adjust accordingly.

    Solution 1: Using Joins

    select distinct e.EmployeeId, e.Name
    from employee e
    left join reviews r1 on e.EmployeeId = r1.EmployeeId and r1.rating = 'good'
    left join reviews r2 on e.EmployeeId = r2.EmployeeId and r1.rating = 'bad'
    where r1.ReviewId is not null --meaning there's at least one
    and r2.ReviewId is null --meaning there's no bad review
    

    Solution 2: Grouping By and Filtering with Conditional Count

    select e.EmployeeId, max(e.Name) Name
    from employee e
    left join reviews r on e.EmployeeId = r.EmployeeId
    group by e.EmployeeId
    having count(case r.rating when 'good' then 1 else null end) > 0
    and  count(case r.rating when 'bad' then 1 else null end) = 0
    

    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.

提交回复
热议问题