2013-09-26 2 views
0

는 내가 두 sce.ACODE 하나가 null이 아닌 수와 위치를 sce.ACODE 다른 하나를 적용 할 필요가 여기에 아래의 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; 

이 null 어떻게 다른 조건으로 같은 열을 계산할 수 있습니다. 도와주세요 SELECT에서 다른 조건

답변

2

와 동일한 열을 요약하는 방법

Edit 

:

은 널 (null)을 계산하려면

:

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

비 - 널 (null)을 계산하기 위해

count(sce.acode) 
아래에 의견을 바탕으로 10

편집 는 요구 사항 때문에, 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) 
+0

+1하지만'ELSE NULL을 사용하는 방법에 대한 '불필요한'CASE'의 기본값은 NULL이기 때문에 중복되어 있습니다. –

+0

@GoatCO True ...하지만 ELSE가없는 경우는 가려워집니다. – Joe

+0

솔루션을 적용 해 주셔서 고맙습니다.하지만 합계 (예 : sce.acode가 null이고 sce.CLIP_POINT가 아닌 다른 값이 0 인 경우) , sum (sce.acode가 null이 아니고 sce.CLIP_POINT 인 경우)를 적용해야합니다. 0 end) 그러면 양쪽 모두에 대해 동일한 결과가 발생합니다. 어떻게 해결할 수 있습니까? – Krushna

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;