【问题标题】:Check for changes in all other columns based on similarities one column (cont.)根据一列的相似性检查所有其他列中的更改(续)
【发布时间】:2015-07-10 16:55:53
【问题描述】:
select sum(case when NumFirstNames <> 1 then 1 else 0 end) as DifferentFirstNames,
   sum(case when NumLastNames <> 1 then 1 else 0 end) as DifferentLastNames,
   sum(case when NumSSN <> 1 then 1 else 0 end) as DifferentSSN,
   sum(case when NumPhone <> 1 then 1 else 0 end) as DifferentPhone       
from (select EncounterId, count(*) as Num,
count(distinct FirstName) as NumFirstNames,
count(distinct LastName) as NumLastNames,
count(distinct SSN) as NumSSN,
count(distinct Phone) as NumPhone
from table t
group by EncounterId) e;

我需要上面的查询按名为 FacilityCode 的表中的另一列进行分组,并按列显示重复 EncounterID 和 NO 缺陷的次数。

此外,除了计数(即第一个查询结果背后的数据)之外,是否可以使用类似构建的查询来提取“缺陷”结果?

查看上一个问题的链接: Check for changes in all other columns based on similarities one column

【问题讨论】:

    标签: mysql sql sql-server-2008


    【解决方案1】:

    是的。只需将列放在子查询中并在外部查询中聚合:

    select FacilityCode,
           sum(case when NumFirstNames <> 1 then 1 else 0 end) as DifferentFirstNames,
           sum(case when NumLastNames <> 1 then 1 else 0 end) as DifferentLastNames,
           sum(case when NumSSN <> 1 then 1 else 0 end) as DifferentSSN,
           sum(case when NumPhone <> 1 then 1 else 0 end) as DifferentPhone       
    from (select EncounterId, FacilityCode, count(*) as Num,
                 count(distinct FirstName) as NumFirstNames,
                 count(distinct LastName) as NumLastNames,
                 count(distinct SSN) as NumSSN,
                 count(distinct Phone) as NumPhone
          from table t
          group by EncounterId, FacilityCode
         ) e
    group by FacilityCode;
    

    我不知道您所说的“提取缺陷结果”是什么意思。但是子查询(可能带有合适的having 子句)会获取每个EncounterId 的信息。

    【讨论】:

    • 这是我第一部分所需要的——谢谢。
    • 我所说的“拉出缺陷”是指拉出导致不同名字、不同姓氏等增加的数据(表中不同的数据)。本质上不是对查询进行分组,而是拉 *
    猜你喜欢
    • 2021-06-27
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多