막혀서 도움 부탁드려요, 계산식 질문입니다! 0 1 1,517

by 정유석 [Oracle 기초] [2017.05.22 11:31:25]


select     
b.P_code,    
b.O_code,    
c.qty b_code,    
b.GB_code,    
max ( decode (a.WEEK_1, b.WEEK, b.qty)) ww01,    
max ( decode (a.WEEK_1+1, b.WEEK, b.qty)) ww02,    
max ( decode (a.WEEK_1+2, b.WEEK, b.qty)) ww03,    
max ( decode (a.WEEK_1+3, b.WEEK, b.qty)) ww04,    
max ( decode (a.WEEK_1+4, b.WEEK, b.qty)) ww05    
from     
(    
select min(to_char(to_date(mc_date,'yyyymmdd'),'yyyyww')) WEEK_1    
from CD_DAY    
where year||month= '201701'   
order by WEEK_1    
)a,(    
    select    
            P_code,    
            O_code O_code,    
            '1TEST' GB_code,    
            WEEK,    
            round(sum(c_qty),0) as qty    
    from rap_table_T    
        where P_day <> '201701'   
        and substr(P_day,1,6) = '201701' 
and open_date = (select max(open_date) from rap_table_T WHERE SUBSTR(P_day,1,6) ='201701')  
group by P_code, O_code, WEEK                                   
union    
    select    
            P_code,    
            O_code O_code,    
            '2TEST' GB_code,    
            WEEK,    
            round(sum(a_qty),0) as qty    
    from rap_table_T    
        where P_day <> '201701'     
            and substr(P_day,1,6) = '201701' 
and open_date = (select max(open_date) from rap_table_T WHERE SUBSTR(P_day,1,6) ='201701')  
group by P_code, O_code, WEEK          
union    
select    
P_code,    
O_code O_code,    
'3TEST'GB_code,    
WEEK,    
NVL(ROUND(LEAST(n_a_qty / NULLIF(n_c_qty, 0), 1) * 100, 0), 0) as qty    
from(    
select    
P_code,    
O_code,    
WEEK,    
nvl(sum(c_qty) over(partition by O_code,substr(WEEK,1,4) order by WEEK),0) n_c_qty,    
nvl(sum(a_qty) over(partition by O_code,substr(WEEK,1,4) order by WEEK),0) n_a_qty    
from(    
    select    
            P_code,    
            O_code,    
            WEEK,    
            nvl(sum(c_qty),0) c_qty,    
            nvl(sum(a_qty),0) a_qty    
            from rap_table_T    
            where substr(P_day,1,6) = '201701' 
and open_date = (select max(open_date) from rap_table_T WHERE SUBSTR(P_day,1,6) ='201701')  
group by P_code, O_code, WEEK    
            order by WEEK           
)    
)    
)b,(    
     select     
             O_code O_code,    
             'b_code' b_code,    
             '2TEST'GB_code,    
             sum(a_qty) as qty    
     from rap_table_T    
     where b_code='STAT' 
        and substr(P_day,1,6) = '201701' 
and open_date = (select max(open_date) from rap_table_T WHERE SUBSTR(P_day,1,6) ='201701')  
group by O_code          
     )c    
where b.O_code = c.O_code (+)    
and b.GB_code = c.GB_code(+)    
group by b.P_code, b.O_code,b.GB_code, c.qty   

 

 

P_code o_code b_code GB_code week01 week02 week03 week04 week05
신발 나이키_에어   판매 84 42 0 0 0
신발 나이키_에어   입고   86      
신발 나이키_에어   판매율 0 68 68 68 68
나이키_에어_A1   판매 30 20 0 0 0
신발 나이키_에어_A1 38 입고 26 25      
신발 나이키_에어_A1   판매율 100 100 100 100 100
신발 나이키_에어_A2   판매 0 0 0 0 0
신발 나이키_에어_A2 23 입고          
신발 나이키_에어_A2   판매율 0 0 0 0 0
신발 나이키_에어_B1   판매 0 80 0 0 0
신발 나이키_에어_B1   입고   88      
신발 나이키_에어_B1   판매율 0 100 100 100 100
신발 나이키_에어_B2   판매 30 0 0 0 0
신발 나이키_에어_B2   입고          
신발 나이키_에어_B2   판매율 0 0 0 0 0
신발 나이키_에어_C1   판매 0 0 0 0 0
신발 나이키_에어_C1 11 입고          
신발 나이키_에어_C1   판매율 0 0 0 0

0

 

데이터입니다

T O TAL을 구하려고하는데 일반 판매 율이 해당 주차에 100이거나, 100이.상은 제외하고

100이하 만 따로 1TEST SUM 2TEST SUM해서

판매 율을 구하고싶은데..

그냥 각 TEST1마다 전부 계산식넣어서 

<>100 을 제외하면 될 것 같은데 너무 하드코딩이라

다른방법이 혹시 있나.. 글을 남깁니다

좀 처럼 아이디어가 나질않네요..

도움부탁드려요 

by 마농 [2017.05.22 13:50:49]

동일 테이블을 너무 반복해서 읽네요. 1번만 읽도록 개선이 필요합니다.
결과테이블 말고 원본 테이블(rap_table_t)의 201701 자료를 보여주세요.
원본 대비 원하는 결과테이블도 보여주세요.
원본테이블의 특징을 설명해 주세요.
각 항목의 의미는?
p_day 에 연월만 들어 있는 자료와 연월일이 들어 있는 자료의 차이는? 의미는?
b_code='STAT' 이 의미하는 것은?
판매율을 구하는 공식이 이해가 안가네요.
왜 판매가 분자가 아닌 분모로 들어가는지 상식적으로 이해가 안가네요.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입