DATA_a | Daily | EA_A | EA_B | INVEN |
png_L_01_DDA | 20170501 | 0 | 19 | Y |
png_L_01_DDA | 20170501 | 20 | 0 | |
png_L_01_DDA | 20170502 | 0 | 20 | |
png_L_01_DDA | 20170503 | 0 | 0 | |
png_L_01_DDA | 20170504 | 0 | 0 | |
png_L_01_DDA | 20170505 | 0 | 0 | |
png_L_01_DDA | 20170506 | 0 | 0 | |
png_L_01_DDA | 20170507 | 15 | 0 | |
png_L_01_DDA | 20470508 | 0 | 0 |
원본데이터입니다. 데이터의 컬럼 데이터값은 전부 varchar2(100Byte)입니다
select
data_a,
daily,
ea_a,
ea_b,
inven
from
(
select 'png_L_01_DDA' DATA_a, '20170501' daily, '0' EA_A, '19' EA_B, 'Y' INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170501' daily, '20' EA_A, '0' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170502' daily, '0' EA_A, '20' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170503' daily, '0' EA_A, '0' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170504' daily, '0' EA_A, '0' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170505' daily, '0' EA_A, '0' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170506' daily, '0' EA_A, '0' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170507' daily, '15' EA_A, '0' EA_B, null INVEN from dual union
select 'png_L_01_DDA' DATA_a, '20170508' daily, '0' EA_A, '0' EA_B, null INVEN from dual
)
해당쿼리이구요 아래의 로우데이터는 엑셀로 수기로 작성한것입니다.
DATA_a | Daily | EA_A | EA_B | 표시되어야할EA_B | INVEN | 차질 및 남은 재고 |
png_L_01_DDA | 20170501 | 0 | 19 | 0 | Y | 19 |
png_L_01_DDA | 20170501 | 20 | 0 | 19 | -1 | |
png_L_01_DDA | 20170502 | 0 | 20 | 1 | 19 | |
png_L_01_DDA | 20170503 | 0 | 0 | 0 | 19 | |
png_L_01_DDA | 20170504 | 0 | 0 | 0 | 19 | |
png_L_01_DDA | 20170505 | 0 | 0 | 0 | 19 | |
png_L_01_DDA | 20170506 | 0 | 0 | 0 | 19 | |
png_L_01_DDA | 20170507 | 15 | 0 | 15 | 4 | |
png_L_01_DDA | 20470508 | 0 | 0 | 0 | 4 |
아래의 데이터처럼 나와야하는데.. 잘 되지가않습니다
DATA_a | Daily | EA_A | EA_B |
png_L_01_DDA | 20170501 | 20 | 19 |
png_L_01_DDA | 20170502 | 0 | 1 |
png_L_01_DDA | 20170503 | 0 | 0 |
png_L_01_DDA | 20170504 | 0 | 0 |
png_L_01_DDA | 20170505 | 0 | 0 |
png_L_01_DDA | 20170506 | 0 | 0 |
png_L_01_DDA | 20170507 | 15 | 15 |
png_L_01_DDA | 20470508 | 0 | 0 |
내용 수정하였습니다.
설명은 EA_A는 주문이고 EA_B에 INVEN 'Y' 면 월초재고이며, null값인 EA_B는 해당일의 생산된제품입니다
해당일에 주문을 생산을 맞추느냐 확인하려고 합니다 그러고 차질된 제품도 확인하려 하고요
5/1 주문은 20인데 5/1에 생산된 제품은 없지만 재고가 19개 존재하므로
5/1은 19개의 주문만 수락되어 차질이 -1개가 발생하였습니다
5/2에는 주문은 없지만 생산된 제품이 20개가 있습니다
하지만 5/1에 차질이 -1이 있으므로 생산된 제품 20개로 확보가 되기때문에 5/2 남은 재고는 19개가 되며
EA_A 는 실질적으로 0이지만 EA_B에서는 1을 보여주어야합니다
INVEN 이란 항목은
5월초 즉 5월1일의 재고입니다.
아래 두개의 표 중 젤 아래것으로 결과물이 나왔으면합니다
중간에있는표는 저런식으로 계산이되어 표시되어야할 EA_B 라는항목이 젤 아래 EA_B로 보여져야합니다
WITH low_data_c_table_cs AS ( SELECT 'png_L_01_DDA' data_a, '20170501' daily, 0 ea_a, 19 ea_b, 'Y' inven FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170501', 20, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170502', 0, 20, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170503', 0, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170504', 0, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170505', 0, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170506', 0, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170507', 15, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170508', 0, 0, '' FROM dual ) SELECT data_a, daily , ea_a -- 주문 , ea_b -- 생산(재고포함) , LEAST(a, b) - LEAST(a - ea_a, b - ea_b) ea_c -- 출고 , b - LEAST(a, b) ea_d -- 재고 FROM (SELECT data_a, daily , SUM(ea_a) ea_a , SUM(ea_b) ea_b , SUM(SUM(ea_a)) OVER(PARTITION BY data_a, SUBSTR(daily, 1, 6) ORDER BY daily) a , SUM(SUM(ea_b)) OVER(PARTITION BY data_a, SUBSTR(daily, 1, 6) ORDER BY daily) b FROM low_data_c_table_cs WHERE data_a ='png_L_01_DDA' AND daily BETWEEN '20170501' AND '20170508' GROUP BY data_a, daily ) ORDER BY data_a, daily ;
WITH low_data_c_table_cs AS ( SELECT 'png_L_01_DDA' data_a, '20170501' daily, 0 ea_a, 0 ea_b, 'Y' inven FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170501', 10, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170502', 10, 10, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170503', 0, 6, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170504', 0, 3, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170505', 0, 6, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170506', 0, 12, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170507', 10, 0, '' FROM dual UNION ALL SELECT 'png_L_01_DDA', '20170508', 10, 0, '' FROM dual ) SELECT data_a, daily , ea_a -- 주문 , ea_b -- 생산(재고포함) , LEAST(a, b) - LEAST(a - ea_a, b - ea_b) ea_c -- 출고 , b - LEAST(a, b) ea_d -- 재고 , b - a ea_e -- 재고 및 차질 FROM (SELECT data_a, daily , SUM(ea_a) ea_a , SUM(ea_b) ea_b , SUM(SUM(ea_a)) OVER(PARTITION BY data_a, SUBSTR(daily, 1, 6) ORDER BY daily) a , SUM(SUM(ea_b)) OVER(PARTITION BY data_a, SUBSTR(daily, 1, 6) ORDER BY daily) b FROM low_data_c_table_cs WHERE data_a ='png_L_01_DDA' AND daily BETWEEN '20170501' AND '20170508' GROUP BY data_a, daily ) ORDER BY data_a, daily ;