현재는 아래와 같이 월중에 입고 출고 재고만을 가져오는데
기초재고를 반영하지 않는 상태입니다.
테이블은 입고. 출고. 그리고 날짜값 20140301 ~ 20140331 같은 테이블이 있구요.
여기에 매월 1일자 재고에 기초재고를 반영하여 누적하고자 합니다.
기초재고만을 별도로 입력해놓은 테이블을 아래와 같이 만들어놓고
매월 1일자 재고에 기초재고를 넣어 합산하고자 하는데 어디에 추가하면 될지 답이 잘 안나오네요..
월별 제품별 기초재고 테이블
----------------------------------------------
20140301 A제품 1000
<현재 결과>
'일자' In_Qty 입고 Out_Qty 출고 Rem_Qty 재고
=======================================================
20140301 16.694 0 16.694
20140302 0 0 16.694
..................
20140330 0 0 34.237
20140331 0 0 34.237
SELECT '일자' ,
0 as in_qty,
0 as out_qty,
TO_NUMBER(A.MONTH_DEADLINE_QTY) as rem_qty
FROM MONTH_DEADLINE A
WHERE A.MONTH_DEADLINE_DATE LIKE '201403%'
AND TRIM(A.CSTM_NO) ='A0006384'
UNION ALL
(SELECT MONTH_DEADLINE_DATE,
NVL
(
(
SELECT SUM(ORD_QTY)
FROM SDISRET2 C
WHERE C.ORD_LC_NO ='보관물품'
AND C.ISSU_REL_DATE = B.MONTH_DEADLINE_DATE
AND TRIM(CSTM_NO) ='A0006384'
),0
) AS ORD_QTY,
SUM(NVL(A.ISSU_QTY,0)) AS ISSU_QTY ,
NVL
(
(SELECT SUM(NVL(ORD_QTY,0))
FROM SDISRET2
WHERE ORD_LC_NO ='보관물품'
AND ISSU_REL_DATE BETWEEN SUBSTR(B.MONTH_DEADLINE_DATE,0,6)||'01'
AND B.MONTH_DEADLINE_DATE
AND TRIM(CSTM_NO) ='A0006384'
),0
)
- NVL
(
(SELECT SUM(NVL(ISSU_QTY,0))
FROM SDISSLT1
WHERE ORD_LC_NO ='보관물품'
AND ISSU_DATE BETWEEN SUBSTR(B.MONTH_DEADLINE_DATE,0,6)||'01'
AND B.MONTH_DEADLINE_DATE
AND TRIM(CSTM_NO) ='A0006384'
) ,0
) AS REM_QRY
FROM SDISSLT1 A,
DAY_DEADLINE B
WHERE A.ORD_LC_NO(+) ='보관물품'
AND A.ISSU_DATE(+) = B.MONTH_DEADLINE_DATE
AND B.MONTH_DEADLINE_DATE(+) LIKE '201403%'
AND TRIM(A.CSTM_NO(+)) ='A0006384'
GROUP BY MONTH_DEADLINE_DATE
)
WITH INVT(YMD, GOODS, INVT_QTY) AS ( SELECT '20140301', 'A제품', 1000 FROM DUAL UNION ALL SELECT '20140301', 'B제품', 300 FROM DUAL ) , STOR(YMD, GOODS, STOR_QTY) AS ( SELECT '20140301', 'A제품', 16 FROM DUAL UNION ALL SELECT '20140301', 'A제품', 20 FROM DUAL UNION ALL SELECT '20140305', 'B제품', 100 FROM DUAL ) , SALE(YMD, GOODS, SALE_QTY) AS ( SELECT '20140301', 'A제품', 500 FROM DUAL UNION ALL SELECT '20140302', 'A제품', 250 FROM DUAL UNION ALL SELECT '20140305', 'B제품', 120 FROM DUAL ) SELECT A.YMD, A.GOODS , STOR_QTY , SALE_QTY , SUM(STOR_QTY) OVER (PARTITION BY A.GOODS ORDER BY YMD) - SUM(SALE_QTY) OVER (PARTITION BY A.GOODS ORDER BY YMD) AS DD_INVT_QTY FROM ( SELECT A.YMD, A.GOODS , SUM(STOR_QTY) AS STOR_QTY , SUM(SALE_QTY) AS SALE_QTY FROM (SELECT YMD, GOODS, INVT_QTY AS STOR_QTY, 0 AS SALE_QTY FROM INVT UNION ALL SELECT YMD, GOODS, STOR_QTY , 0 AS SALE_QTY FROM STOR UNION ALL SELECT YMD, GOODS, 0 AS INVT_QTY , SALE_QTY FROM SALE UNION ALL SELECT '20140331', 'A제품' AS GOODS, 0, 0 FROM DUAL UNION ALL --월말 나오게 하기 위해서(필요 없으면 안해도 됨) SELECT '20140331', 'B제품' AS GOODS, 0, 0 FROM DUAL --월말 나오게 하기 위해서(필요 없으면 안해도 됨) ) A GROUP BY A.YMD, A.GOODS ) A ORDER BY A.GOODS, A.YMD;