简体   繁体   中英

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. 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 '='

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:

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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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