简体   繁体   English

case = 1 时的条件计数,DB2

[英]Conditional count when case = 1, DB2

I'm currently trying to figure out the best way to do a conditional count as an alias in DB2 for Iseries.我目前正在尝试找出在 Iseries 的 DB2 中将条件计数作为别名的最佳方法。 The below values represent job statuses where a job can be created, completed and cancelled so any one job will possibly have multiple status codes attached to it.以下值表示可以创建、完成和取消作业的作业状态,因此任何一项作业都可能附加多个状态代码。

However, for my final value, I'm trying to get a count of jobs that only have the created status so that I can show how many are still open jobs.但是,对于我的最终价值,我正在尝试计算仅具有已创建状态的工作,以便我可以显示仍有多少工作处于开放状态。 Basically looking for cases where the count for the created case = 1, but the below fails at the '='基本上寻找已创建案例的计数 = 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;

sample data and results:样本数据和结果:

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

Results:结果:

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

Assuming that the only "close" status is 'CANCELLED' , you can use not exists like this:假设唯一的“关闭”状态是'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')
                 );

If you want multiple counts, then filtering like this does not work.如果您想要多个计数,那么像这样的过滤不起作用。 So aggregate by job first:所以先按工作汇总:

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

With conditional aggregation:使用条件聚合:

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 

See the demo .请参阅演示
Results:结果:

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

The following returns the result you need:以下返回您需要的结果:

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;

Assuming valid close status is either "COMPLETED" or "CANCELLED", you can try following SQL.假设有效的关闭状态是“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 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM