case = 1 时的条件计数,DB2

Conditional count when case = 1, DB2

我目前正在尝试找出在 DB2 for Iseries 中作为别名进行条件计数的最佳方法。以下值表示可以创建、完成和取消作业的作业状态,因此任何一个作业都可能附加多个状态代码。

但是,对于我的最终值,我正在尝试计算仅具有已创建状态的职位,以便我可以显示有多少职位仍处于空缺状态。基本上是在寻找创建案例的计数 = 1 的案例,但下面的案例在 '='

处失败
SELECT
  COUNT(CASE A1.JOB WHEN = 'CREATED' THEN 1 END) AS CREATED,
  COUNT(CASE A1.JOB WHEN = 'CANCELLED' THEN 1 END) AS CANCELLED,
  COUNT(CASE WHEN A1.JOB 'CREATED' = 1 then 1  END)  AS OPEN
FROM SCHEMA.TABLE A1;

示例数据和结果:

    Job ID   |   Status_code
-------------------------
123         'CREATED'
123         'COMPLETED'
521         'CREATED'
521         'CANCELLED'
645         'CREATED'

结果:

JOB  |  CREATED  |   CANCELLED   |   OPEN
-------------------------------------------
123     1               0               0
521     1               1               0
645     1               0               1

假设唯一的"close"状态是'CANCELLED',你可以这样使用not exists

select count(*)
from schema.table t
where t.status_code = 'CREATED' and
      not exists (select 1
                  from schema.table t2
                  where t2.job = t.job and
                        t2.status_code in ('CANCELLED', 'COMPLETED', 'DELETED')
                 );

如果你想要多次计数,那么这样的过滤是行不通的。所以先按工作汇总:

select sum(is_created) as num_created,
       sum(is_cancelled) as num_cancelled,
       sum(is_created * (1 - is_cancelled) * (1 - is_completed) * (1 - is_deleted)) as open
from (select job,
             max(case when status_code = 'CREATED' then 1 else 0 end) as is_created,
             max(case when status_code = 'CANCELLED' then 1 else 0 end) as is_cancelled,
             max(case when status_code = 'COMPLETED' then 1 else 0 end) as is_completed,
             max(case when status_code = 'DELETED' then 1 else 0 end) as is_deleted
      from t
      group by job
     ) j

有条件聚合:

SELECT
  JobID,
  MAX(CASE Status_code WHEN 'CREATED' THEN 1 ELSE 0 END) AS CREATED,
  MAX(CASE Status_code WHEN 'CANCELLED' THEN 1 ELSE 0 END) AS CANCELLED,
  MIN(CASE WHEN Status_code <> 'CREATED' THEN 0 ELSE 1 END) AS OPEN
FROM tablename 
GROUP BY JobID 

参见demo
结果:

> JobID | CREATED | CANCELLED | OPEN
> ----: | ------: | --------: | ---:
>   123 |       1 |         0 |    0
>   521 |       1 |         1 |    0
>   645 |       1 |         0 |    1

以下returns您需要的结果:

WITH TAB (Job_ID, JOB) AS 
(
VALUES
  (123, 'CREATED')
, (123, 'COMPLETED')
, (521, 'CREATED')
, (521, 'CANCELLED')
, (645, 'CREATED')
)
SELECT
  Job_ID
, COUNT(CASE A1.JOB WHEN 'CREATED'   THEN 1 END) AS CREATED
, COUNT(CASE A1.JOB WHEN 'CANCELLED' THEN 1 END) AS CANCELLED
, CASE 
    WHEN NULLIF(COUNT(1), 0) = COUNT(CASE A1.JOB WHEN 'CREATED' then 1 END) 
    THEN 1 
    ELSE 0 
  END AS OPEN
FROM TAB A1
GROUP BY JOB_ID;

假设有效的关闭状态是 "COMPLETED" 或 "CANCELLED",您可以尝试以下 SQL.

SELECT
  A1.JobID,
  sum(CASE  WHEN A1.Status_code = 'CREATED' THEN 1 ELSE 0 END) AS CREATED,
  sum(CASE  WHEN  A1.Status_code = 'CANCELLED' THEN 1 ELSE 0 END) AS CANCELLED,
  (
     SUM(CASE WHEN A1.Status_code = 'CREATED' THEN 1 ELSE 0 END)
   - sum(CASE WHEN A1.Status_code = 'CANCELLED' THEN 1 ELSE 0 END)
   - sum(CASE WHEN A1.Status_code = 'COMPLETED' THEN 1 ELSE 0 END)
 ) AS OPEN
FROM SCHEMA.TABLE A1
GROUP BY A1.JobID