简体   繁体   English

如何过滤出至少出现一次值的组?

[英]How do I filter out a group where there is at least one occurrence of a value?

In sample data below I need to show only records where the ContactType is not equal to 'CLOSR' by caseid_i but with my code below it filters out anything with CLOSR, but not by the CaseID_I.在下面的示例数据中,我只需要通过caseid_i显示ContactType不等于 'CLOSR' 的记录,但在下面的代码中,它使用 CLOSR 过滤掉任何内容,但不通过 CaseID_I 过滤掉任何内容。 So if at least one of the Caseid_i has a ContactType of 'CLOSR' I need to filter out the whole CaseID.因此,如果 Caseid_i 中的至少一个具有“CLOSR”的 ContactType,我需要过滤掉整个 CaseID。 So for CaseID_i 51709, that caseid_i wouldn't show up in my results at all because at least one of the 'ContactTypes' is CLOSR and the same for 51715.因此,对于 CaseID_i 51709,caseid_i 根本不会出现在我的结果中,因为至少有一个“ContactTypes”是 CLOSR,而 51715 也是如此。

在此处输入图片说明

SELECT C.[cdcpincid_c]
    ,I.[caseid_i]
    ,I.echono_c
    ,C.[close_d] AS ClosureDate
    ,C.[clinician_c] AS ClosureClinician
    ,N.[contacttyp_c] AS ContactType
    ,SC.StaffName AS ClosureStaffName
    ,I.Refdate_d
FROM [cd].[tb_cdcp_case] C
INNER JOIN [cd].[tb_cdcp_incident] I ON I.[uniqueid_c] = C.[cdcpincid_c]
INNER JOIN [cd].[tb_cdcp_clientcontact] N ON I.[uniqueid_c] = N.[cdcpincid_c]
LEFT OUTER JOIN [dbo].[vCDCP_Staff] SC ON C.clinician_c = SC.Staffcode_c
GROUP BY C.[cdcpincid_c]
    ,I.[caseid_i]
    ,I.echono_c
    ,C.[close_d]
    ,C.[clinician_c]
    ,N.[contacttyp_c]
    ,SC.StaffName
    ,I.Refdate_d
HAVING sum(CASE 
            WHEN N.[contacttyp_c] = 'CLOSR'
                THEN 1
            ELSE 0
            END) = 0
    AND C.[close_d] IS NOT NULL
    AND I.echono_c <> 'OC'

Remove the condition from the where clause and switch it to a having clause:where子句中删除条件并将其切换为having子句:

having sum(case when ContactType = 'CLOSR' then 1 else 0 end) = 0

EDIT:编辑:

I see.我知道了。 You are returning multiple rows per caseid .您正在为每个caseid返回多行。 In that case, use a window function as well:在这种情况下,也可以使用窗口函数:

having sum(sum(case when ContactType = 'CLOSR' then 1 else 0 end)) over (partition by caseid) = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL Group By:如果至少一行包含值,如何排除组? - MySQL Group By: How exclude group if at least one row contain value? 如何过滤数字范围内的SQL行? - How do I filter out SQL rows where numbers range? 如果组中至少有一条记录满足特定条件,如何编写查询以排除记录组? - How do I write a query to exclude groups of records if at least one record in the group meets a certain condition? SQL-筛选出一个完整的组,该组的至少一个成员不属于特定结果 - SQL - Filter out a complete group that has at least one member not part of a specific result 在至少一行满足条件的情况下,如何返回所有行的值? - How can I return all rows for a value where at least one row meets a condition? 按组检查每个唯一值是否至少有一行,其中该字段的值等于另一个字段的值 - By group, check if for each unique value, there is at least one row where the value of the field equals another field's value SQL:如何将列合并为一,并过滤出NULL? - SQL: How do I combine columns into one, and filter out NULLs? 如何检查一组行中的至少一个是否具有特定值 - How to check if at least one of a group of rows has a specific value 如何根据合并列的值过滤分组查询? - How do I filter a group-by query on the value of a coalesced column? rails:至少有一篇文章的过滤器类别 - rails: filter categories where there are at least one article
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM