0

如果我有一个包含如下数据的表:

Search Criteria Table
=================================
Group              Name
=======            =======
Surgeon            Hannibal
Anesthesiologist   Murdock
Perfusionist       Face
Nurse              BA

Data Tables
=================================
CaseId      Patient
=========   ===========
1           Colonel Lynch
2           Roderick Decker
3           Captain Crane

CaseId     PersonnelGroup       PersonnelName
=========  ================     =============
1          Surgeon              Hannibal
1          Anesthesiologist     Tawnia Baker
2          Perfusionist         Face
2          Surgeon              Hannibal
2          Anesthesiologist     Murdock
2          Nurse                BA
3          Surgeon              Frankie Santana
3          Anesthesiologist     Murdock
3          Nurse                BA
4          Surgeon              Frankie Santana
4          Anesthesiologist     Tawnia Baker
4          Perfusionist         Amy Allen
4          Nurse                BA
5          Surgeon              Hannibal
5          Anesthesiologist     Murdock
5          Perfusionist         Face
5          Nurse                BA

我想编写一个程序,该程序将根据标志向我显示这些人员中的任何一个人处理过该案例的案例,或者他们所有人都从事过该案例的工作。希望我在上面添加的数据反映了这一点,但我想在设置“where any”模式时返回所有 5 种情况,并且只在设置“where all”模式时返回情况 5。

我知道如果我将 3 个表连接在一起,我会回答“任何地方”部分,但不确定如何做“所有地方”部分。那有意义吗?

是否可以在单个语句中执行?我通过传入我拆分成表的分组对列表来获取搜索条件表。我可以控制数据如何传递到 proc 中,因此如果需要更改它也可以。

4

1 回答 1

0

这是一种方法:

select c.caseId
from cases c 
     SearchCriteria sc left outer join
     on sc.PersonnelName = c.Name and
        sc.PersonnelGroup = c.Group
group by c.caseId
having (@flag = 'all') and (count(*) = (select COUNT(sc.PersonnelName) from SearchCriteria) or
       (@flag = 'any') and count(sc.PersonnelName) > 0
于 2013-04-15T21:31:19.370 回答