select p.proType, p.proCode, p.proStatus, p.proPFrom, p.proPTo,
nvl(q.bRt, 0) pRt, nvl(r.cRt, 0) aRt
from pro_mst p
left outer join (select proCode, sum(mrtPRate) bRt
from pro_mrt
where mrtYMD <= '200506'
group by proCode) q on p.proCode = q.proCode
left outer join (select proCode, sum(mrtARate) cRt
from pro_amrt
where mrtYMD <= '200506'
group by proCode) r on p.proCode = r.proCode
==>결과
PROTYPE PROCODE PROSTATUS PROPFROM PROPTO PRT ART
3 PRO200505060001 1 20050101 20071201 27 5
1 PRO200505230002 1 20050502 20050530 0 0
select x.proType, nvl(count(x.proCode), 0) bCnt
from (select p.proType, p.proCode, p.proStatus, p.proPFrom, p.proPTo,
nvl(q.bRt, 0) pRt, nvl(r.cRt, 0) aRt
from pro_mst p
left outer join (select proCode, sum(mrtPRate) bRt
from pro_mrt
where mrtYMD <= '200506'
group by proCode) q on p.proCode = q.proCode
left outer join (select proCode, sum(mrtARate) cRt
from pro_amrt
where mrtYMD <= '200506'
group by proCode) r on p.proCode = r.proCode
) x
where x.proStatus = '1'
and x.pRt <= x.aRt
group by x.proType
==> 결과
PROTYPE BCNT
1 1
select a.pType, a.proTypeName, nvl(a.aCnt, 0) aCnt, nvl(b.bCnt, 0) bCnt
from (select '1' pType, '연구' proTypeName, nvl(count(proCode), 0) aCnt
from pro_mst
where proType = '1'
union
select '2' pType, '개발' proTypeName, nvl(count(proCode), 0) aCnt
from pro_mst
where proType = '2'
union
select '3' pType, '상용화' proTypeName, nvl(count(proCode), 0) aCnt
from pro_mst
where proType = '3') a
left outer join
(select x.proType, nvl(count(x.proCode), 0) bCnt
from (select p.proType, p.proCode, p.proStatus, nvl(q.bRt, 0.00) pRt, nvl(r.cRt, 0.00) aRt
from pro_mst p
left outer join (select proCode, sum(mrtPRate) bRt
from pro_mrt
where mrtYMD <= '200506'
group by proCode) q on p.proCode = q.proCode
left outer join (select proCode, sum(mrtARate) cRt
from pro_amrt
where mrtYMD <= '200506'
group by proCode) r on p.proCode = r.proCode
) x
where x.proStatus = '1'
and x.pRt <= x.aRt
group by x.proType ) b on a.pType = b.proType
==> 결과
PTYPE PROTYPENAME ACNT BCNT
1 연구 2 1
3 상용화 1 1
2 개발 0 0
이런 결과가 나옵니다
위 쿼리문을 보시면 아시겠지만
and x.pRt <= x.aRt <-- 이 조건때문애 상용화의 BCNT에는 값이 들어가면 안되는데 값이 나옵니다
왜그럴까요?
한쪽테이블을 기준으로 모두 나오는거 아닌가요?
그리고 아래쪽에 union을 쓰셨는데...
decode나 case when 문장과 조건절에 in연산자를 쓰셔서 하시는게 더 낳을듯 싶습니다.
그리고 중복레코드를 제거할 필요가 없다면 union 보다는 union all이 더 좋지않나요?
그럼..