如果我有一个包含以下数据的表:
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中,所以如果需要更改它也可以。
答案 0 :(得分: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