0

我有以下 SQL 查询

  SELECT 
  max (bt.NAME) "Batch Type",
  max(st.name) "Submission Status",
  count(sce.CLIP_POINT)--where sce.ACODE is not null
  ,count(sce.CLIP_POINT)--where sce.ACODE is null
  ,sum(sce.CLIP_POINT)--where sce.ACODE is not null
  ,sum(sce.CLIP_POINT)--where sce.ACODE is null
  from SUBMISSION_DATA_ENTRY sde,
  BATCH_PROCESSOR bp, status st,SUBMISSION_CLIP_ENTRY sce,
  SUBMISSION_PERIOD sp,BATCH_TYPE bt
  where sde.sub_perd_id=sp.sub_perd_id 
  and sde.bpr_pk=bp.bpr_pk and sde.sts_pk=st.sts_pk
  and sce.sde_pk=sde.sde_pk 
  and bt.bat_pk =bp.bat_pk
  group by bt.bat_pk, st.sts_pk;

在这里,我需要应用两个计数,一个用于 sce.ACODE 不为空,另一个用于 sce.ACODE 为空。我如何计算具有不同条件的同一列。请帮忙

Edit

如何对具有不同条件的同一列求和

4

2 回答 2

2

在选择中:

要计算空值:

count (case when sce.acode is null then 1 else null end)

计算非空值:

count(sce.acode)

编辑 根据下面的评论,要求是对 clip_point 求和,所以:

-- sum of clip_point when acode is null
sum(case when sce.acode is null then sce.clip_point else 0 end)

 -- sum of clip_point when acode is not null 
sum(case when sce.acode is null then 0 else sce.clip_point end)
于 2013-09-26T16:27:10.780 回答
1

使用总和怎么样?:

SELECT 
  max (bt.NAME) "Batch Type",
  max(st.name) "Submission Status",
  sum(case when sce.CLIP_POINT is not null then 1 else 0 end)--where sce.ACODE is not null
  ,sum(case when sce.CLIP_POINT is null then 1 else 0 end)--where sce.ACODE is null
  from SUBMISSION_DATA_ENTRY sde,
  BATCH_PROCESSOR bp, status st,SUBMISSION_CLIP_ENTRY sce,
  SUBMISSION_PERIOD sp,BATCH_TYPE bt
  where sde.sub_perd_id=sp.sub_perd_id 
  and sde.bpr_pk=bp.bpr_pk and sde.sts_pk=st.sts_pk
  and sce.sde_pk=sde.sde_pk 
  and bt.bat_pk =bp.bat_pk
  group by bt.bat_pk, st.sts_pk;
于 2013-09-26T16:27:27.223 回答