【问题标题】:Count Dimension Items that All Have the Same Value in Another Field计算另一个字段中所有具有相同值的维度项
【发布时间】:2019-07-11 07:50:41
【问题描述】:

我有一个如下所示的表格: business_id、employee_id 和状态,“活动”或“非活动”

我想计算所有员工都“活跃”的企业数量。完成此任务的最佳方法是什么?

【问题讨论】:

    标签: sql


    【解决方案1】:

    这是一种方法:

    select count(distinct business_id)
    from t
    where not exists (select 1
                      from t t2
                      where t2.business_id = t.business_id and
                            t2.status <> 'active'
                     );
    

    或者,两级聚合:

    select count(*)
    from (select business_id
          from t
          group by business_id
          having min(status) = max(status) and min(status) = 'active'
         ) b;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多