decode 관련해서 문의 드립니다 0 4 7,893

by 손님 decode oracle [2009.04.24 15:11:53]


각 부서에서 문서를 작성할때 문서마다 형식이 있고(type 1, 2 이런식으로 )
그형식마다 기준매수를 정해 놓고 기준매수대로 작성했을때는 0,  초과했을때는 초과된매수를 적어줍니다.   

부서명  type   page  
총무     1       0  
총무     1       3
총무     1       1
총무     2       0
총무     2       0
총무     2       4
총무     2       5

이렇게 데이터가 들어간다고 했을때 

부서이름  type1총건수  type1기준이내건수   type1초과건수    type2총건수     type2기준이내건수   type2초과건수
총무                 3                          1                                  2                         4                             2                            2

이런식으로 데이터를 뽑고 싶은데

select dept_name
,'type1 총건수   : ' || count(decode(type,1,1)) as type1_total
,'type1 기준이내 : ' || count(decode(page,0,decode(type, 1, 1))) as type1_기준이내
,'type2 총건수   : ' || count(decode(type,2,1)) as type2_total
,'type1 기준이내 : ' || count(decode(page,0,decode(type, 2, 1))) as type2_기준이내
from test
where  dept_name='총무부'
group by  dept_name ;

이렇게 하면 우선
부서이름  type1총건수  type1기준이내건수   type1초과건수    type2총건수     type2기준이내건수   type2초과건수
총무                    3                           1                                                            4                           2                

이렇게 나오는데여 초과건수를 어떻게 뽑아야 할지 ^^:  우선 타입이 1 인것을 찾고 그중에서 page가 0  이 아닌것의

count 를 해야한는데  count(decode(page,0,decode(type, 1, 1),0,1))  이렇게  하면 될꺼라 생각했는데

결과가 안나오더라구여 ㅡㅡㅋ  총건수에서 기준이내 건수를 빼도 되지만 그냥 쿼리로서 해결해 보고 싶은데

고수님들의 조언 부탁드립니다.

by 서성우 [2009.04.24 15:22:22]
WITH test AS
(
SELECT '총무부' dept_name , 1 TYPE , 0 page FROM dual
UNION ALL SELECT '총무부' , 1 , 3 FROM dual
UNION ALL SELECT '총무부' , 1 , 1 FROM dual
UNION ALL SELECT '총무부' , 2 , 0 FROM dual
UNION ALL SELECT '총무부' , 2 , 0 FROM dual
UNION ALL SELECT '총무부' , 2 , 4 FROM dual
UNION ALL SELECT '총무부' , 2 , 5 FROM dual
)
select dept_name
,'type1 총건수 : ' || count(decode(type,1,1)) as type1_total
,'type1 기준이내 : ' || count(decode(page,0,decode(type, 1, 1))) as type1_기준이내
,'type1 초과이내 : ' || Count(CASE WHEN (page > 0 AND TYPE = 1) THEN page END) AS type1_초과건수
,'type2 총건수 : ' || count(decode(type,2,1)) as type2_total
,'type2 기준이내 : ' || count(decode(page,0,decode(type, 2, 1))) as type2_기준이내
,'type2 초과이내 : ' || Count(CASE WHEN (page > 0 AND TYPE = 2) THEN page END) AS type2_초과건수
from test
where dept_name='총무부'
group by dept_name ;

저런건 decode말고
case문으로 해결을 하시는게 좋을것 같네요

by 서성우 [2009.04.24 15:28:11]
꼭 decode를 사용하셔서 해야 한다면

WITH test AS
(
SELECT '총무부' dept_name , 1 TYPE , 0 page FROM dual
UNION ALL SELECT '총무부' , 1 , 3 FROM dual
UNION ALL SELECT '총무부' , 1 , 1 FROM dual
UNION ALL SELECT '총무부' , 2 , 0 FROM dual
UNION ALL SELECT '총무부' , 2 , 0 FROM dual
UNION ALL SELECT '총무부' , 2 , 4 FROM dual
UNION ALL SELECT '총무부' , 2 , 5 FROM dual
)
select dept_name
,'type1 총건수 : ' || count(decode(type,1,1)) as type1_total
,'type1 기준이내 : ' || count(decode(page,0,decode(type, 1, 1))) as type1_기준이내
,'type1 초과건수?: ' || Count(Decode(page,0,NULL,Decode(TYPE,1,1))) AS type1_초과건수
,'type2 총건수 : ' || count(decode(type,2,1)) as type2_total
,'type2 기준이내 : ' || count(decode(page,0,decode(type, 2, 1))) as type2_기준이내
,'type2 초과건수 : ' || Count(Decode(page,0,NULL,Decode(TYPE,2,1))) AS type2_초과건수
from test
where dept_name='총무부'
group by dept_name ;

by 마농 [2009.04.24 15:40:50]
SELECT dept_name
, MIN(DECODE(type,1,cnt_tot)) type1_total
, MIN(DECODE(type,1,cnt_1)) type1_기준이내
, MIN(DECODE(type,1,cnt_2)) type1_기준초과
, MIN(DECODE(type,2,cnt_tot)) type2_total
, MIN(DECODE(type,2,cnt_1)) type2_기준이내
, MIN(DECODE(type,2,cnt_2)) type2_기준초과
FROM
(
SELECT dept_name, type
, COUNT(*) cnt_tot
, COUNT(DECODE(page,0,1)) cnt_1
, COUNT(DECODE(SIGN(page),1,1)) cnt_2
FROM test
GROUP BY dept_name, type
)
GROUP BY dept_name
;

by 태기 [2009.04.24 15:45:04]
서성우님 마농님 정말 감사합니다
덕분에 해결했습니다. 아직 갈길이 멀지만 ^^;
case 문은 한번도 써본적이 없고 생소해서 decode 로 우선은 했는데
더 공부해야할것 같아여 간단하면서도 어려운것 같습니다 ㅡㅡㅋ
다시 한번 감사드립니다 ^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입