union all 을 없이 조회 가능할까요? 0 2 1,655

by 짱짱 [2009.04.17 18:05:18]


group by 의 결과에서  다시 조건문을 줘서 합을 구해야합니다.
데이터 건수가 많아서 union all을 써서 결과를 내는게 좀 부담스럽네요.

아래 쿼리가 적당한지 좀 봐주세요. ^^;

SELECT 'A' as div, sum(cnt_2008), sum(cnt_2009) 
FROM
   (       
         SELECT  
                  a.cappart,                   
                  COUNT(DECODE(substr(a.conid,1,4), '2008', a.perid)) cnt_2008,        
                  COUNT(DECODE(substr(a.conid,1,4), '2009', a.perid)) cnt_2009               
         FROM        
                  sbctct a                            
         GROUP BY        
                  a.cappart   
)where cappart in (1,20)
union all
SELECT 'B' as div, sum(cnt_2008), sum(cnt_2009) 
FROM
   (       
         SELECT  
                  a.cappart,               
                  COUNT(DECODE(substr(a.conid,1,4), '2008', a.perid)) cnt_2008,        
                  COUNT(DECODE(substr(a.conid,1,4), '2009', a.perid)) cnt_2009               
         FROM        
                  sbctct a                            
         GROUP BY        
                  a.cappart   
)where cappart in (12, 5, 3)

 

by 채용근 [2009.04.17 18:11:07]
DECODE 로 CAPPART를 나눌수 있을꺼 같아요

by 서성우 [2009.04.17 18:25:13]
SELECT Decode(capart,1,'A',20,'A','B') div,
Sum(cnt_2008),
Sum(cnt_2009)
FROM
(
SELECT cappart,
COUNT(DECODE(substr(conid,1,4), '2008', perid)) cnt_2008,
COUNT(DECODE(substr(conid,1,4), '2009', perid)) cnt_2009
FROM sbctct
where cappart in (1,3,5,12,20)
GROUP BY cappart
)
GROUP BY Decode(capart,1,'A',20,'A','B')
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입