T-SQL:选择一个通用 ID,其中该 ID 匹配某些条件

问题描述 投票:0回答:1

在屏幕截图中,

Id
是主键。此结果集中可能有许多相同的
Participant__c
值,每个值都有不同的
Id

我想做的是编写一个查询,如果特定的

Participant__c
在此结果集中有记录,其中
Participant__c
值是
Survey_Index_Number__c
2001
2003,则该查询将返回唯一的 
2005
,其对应的
Completion_Status__c
值为
Complete
。如果
Participant__c
值与该条件匹配,则在结果集中返回该
Participant__c
值。

我只是很难找出解决此问题的最佳方法,并且我倾向于利用分区逻辑来做到这一点,但我不确定如何开始。

enter image description here

sql t-sql partition partition-by
1个回答
0
投票

假设您需要唯一的 Participant__c 值,如果表/结果集对于每个 Survey_Index_Number__c = 2001、20023、2005 且 Completion_Status__c='Complete' 至少有一行:

select Participant__c
from mytbl a
where  Survey_Index_Number__c in ('2001','2003','2005') 
and Completion_Status__c='Complete'
group by Participant__c
having count(distinct Survey_Index_Number__c )=3
© www.soinside.com 2019 - 2024. All rights reserved.