如何在1个查询中选择1列的计数

时间:2015-03-23 13:01:21

标签: db2

我想将以下查询作为单个查询。

我的意图是获得job_status = A,其中job_status = A和总数的计数。

select 
    WORKSTATION_NAME AS CPU,
    count(*) AS TOTAL 
from 
    MDL.JOB_HISTORY_V 
where 
    MONTH(JOB_RUN_DATE_TIME) = 01 
    AND YEAR(JOB_RUN_DATE_TIME) = 2015 
GROUP BY 
    WORKSTATION_NAME

select 
    WORKSTATION_NAME AS CPU,
    count(*) AS SUCCESS_RUNS 
from 
    MDL.JOB_HISTORY_V 
where 
    JOB_STATUS = 'S' 
    and MONTH(JOB_RUN_DATE_TIME) = 01 
    AND YEAR(JOB_RUN_DATE_TIME) = 2015 
GROUP BY 
    WORKSTATION_NAME

select 
    WORKSTATION_NAME AS CPU, 
    count(*) AS ABEND_RUNS 
from 
    MDL.JOB_HISTORY_V 
where 
    JOB_STATUS = 'A' 
    and MONTH(JOB_RUN_DATE_TIME) = 01 
    AND YEAR(JOB_RUN_DATE_TIME) = 2015 
GROUP BY 
    WORKSTATION_NAME

1 个答案:

答案 0 :(得分:0)

您可以使用以下条件计算:

select WORKSTATION_NAME AS CPU
    ,  count(*) AS TOTAL
    ,  count(case when JOB_STATUS = 'S' then 1 end) as SUCCESS_RUNS
    ,  count(case when JOB_STATUS = 'A' then 1 end) as ABEND_RUNS 
from MDL.JOB_HISTORY_V 
where MONTH(JOB_RUN_DATE_TIME) = 01 
  AND YEAR(JOB_RUN_DATE_TIME) = 2015 
GROUP BY WORKSTATION_NAME