TDEPT
DEPT_CD | DEPT_NM | P_DEPT |
---|---|---|
000000 | 사장실 | |
AA0001 | 경영지원 | 000000 |
AB0001 | 재무 | AA0001 |
AC0001 | 총무 | AA0001 |
BA0001 | 기술지원 | 000000 |
BB0001 | H/W지원 | BA0001 |
BC0001 | S/W지원 | BA0001 |
CA0001 | 영업 | 000000 |
CB0001 | 영업기획 | CA0001 |
CC0001 | 영업1 | CA0001 |
CD0001 | 영업2 | CA0001 |
SELECT LPAD(DEPT_CD,LEVEL*5,' '),DEPT_NM
FROM TDEPT
CONNECT BY PRIOR DEPT_CD=P_DEPT
START WITH DEPT_CD='000000'
DEPT_CD DEPT_NM
------------------------
0000000 사장실
AA0001 경영지원
AB0001 재무
AC0001 총무
BA0001 기술지원
BB0001 H/W지원
BC0001 S/W지원
CA0001 영업
CB0001 영업기획
CC0001 영업1
CD0001 영업2
SELECT LPAD(DEPT_CD,LEVEL*5,' '),DEPT_NM
FROM TDEPT
WHERE DEPT_CD<>'CA0001'
CONNECT BY PRIOR DEPT_CD=P_DEPT
START WITH DEPT_CD='000000'
DEPT_CD DEPT_NM
------------------------
0000000 사장실
AA0001 경영지원
AB0001 재무
AC0001 총무
BA0001 기술지원
BB0001 H/W지원
BC0001 S/W지원
(없슴)
CB0001 영업기획
CC0001 영업1
CD0001 영업2
SELECT LPAD(DEPT_CD,LEVEL*5,' '),DEPT_NM
FROM TDEPT
CONNECT BY PRIOR DEPT_CD=P_DEPT
AND DEPT_CD<>'CA0001'
START WITH DEPT_CD='000000'
DEPT_CD DEPT_NM
------------------------
0000000 사장실
AA0001 경영지원
AB0001 재무
AC0001 총무
BA0001 기술지원
BB0001 H/W지원
BC0001 S/W지원
SELECT LPAD(DEPT_CD,LEVEL*5,' '),DEPT_NM
FROM TDEPT
CONNECT BY PRIOR P_DEPT=DEPT_CD
START WITH DEPT_CD='CD0001'
DEPT_CD DEPT_NM
------------------------
CD0001 영업2
CA0001 영업
0000000 사장실
SELECT LPAD(DEPT_CD,LEVEL*5,' '),DEPT_NM,P_DEPT,PRIOR DEPT_CD
FROM TDEPT
CONNECT BY PRIOR DEPT_CODE=PARENT_DEPT
START WITH DEPT_CODE='000000'
DEPT_CD DEPT_NM P_DEPT PRIOR DEPT_CD
-------------------------------------------------
00000 사장실
AA0001 경영지원 000000 000000
AB0001 재무 AA0001 AA0001
AC0001 총무 AA0001 AA0001
BA0001 기술지원 000000 000000
BB0001 H/W지원 BA0001 BA0001
BC0001 S/W지원 BA0001 BA0001
CA0001 영업 000000 000000
CB0001 영업기획 CA0001 CA0001
CC0001 영업1 CA0001 CA0001
CD0001 영업2 CA0001 CA0001
SELECT LPAD(' ',2,*LEVEL)||X.부품코드,X.THDYFID,Y.부품명
FROM 부품구조 X,부품 Y
WHERE X.부품코드=Y.부품코드
CONNECT BY PRIOR X.부품코드=X.상위부품코드
START WITH X.부품코드='PA101'
SELECT LPAD(' ',2*LV)||X.부푸코드,X.소요량,Y.부품명
FROM
(
SELECT LEVEL LV,부품코드,소요량
FROM 부품구조
CONNECT BY PRIOR 부품코드=상위부품코드
START WITH 부품코드='PA101'
) X,부품 Y
WHERE Y.부품코드=X.부품코드
SELECT
LPAD(' ',2*LVL)||DECODE(SW,'2',대체부품코드,원부품코드) 부품코드 --SW=2인 경우 대체부품을 출고한다
,소요량
,DECODE(SW,'2',대체부품명,원부품명) 부품명
,DECODE(SW,'1','원부품출고','2','대체부품출고','긴급구매') 조달상태
FROM
(
SELECT
LVL,X.부품코드 원부품코드,Z.부품코드 대체부품코드,소요량
,DECODE(
LEAST(Y.안전재고,Y.현재고),Y.안전재고,'1' --Y.안전재고 <= Y.현재고
,DECODE( --Y.안전재고 > Y.현재고 이므로 대체부품을 찾는다
LEAST(Z.안전재고,Z.현재고),Z.안전재고,'2' --Z.안전재고 <= Z.현재고
,'3' --Z.안전재고 > Z.현재고 이므로 긴급상황!!!
)
) SW
,X.부품명 원부품명
,Z.부품명 대체부품명
FROM
(
--PA101과 그 하위부품에 대한 순전개
SELECT
LEVEL LVL,부품코드,소요량
FROM 부품구조
CONNECT BY PRIOR 부품코드=상위부품코드
START WITH 부품코드='PA101'
) X,부품 Y,부품 Z
WHERE Y.부품코드=X.부품코드
--대체부품은 없을 수도 있으므로 아웃조인
AND Z.부품코드(+)=Y.대체부품코드
)
SELECT ......
FROM 부품 Y
(
SELECT .....
FROM 부품구성
CONNECT BY .....
START WITH .....
) X
,(
SELECT --INDEX(W 부품우선순위_DIX)
부품코드,NVL(MAX(현재고),0) 현재고
FROM 대체품목 W
WHERE 현재고>=안전재고
AND ROWNUM=1
) Z
WHERE Y.부품코드=X.부품코드
AND Z.부품코드(+)=Y.부품코드
CREATE TABLE PART_MST( --부품테이블
PART_CD VARCHAR2(10) CONSTRAINT PART_MST_PK PRIMARY KEY,
PART_NM VARCHAR2(10),
CURR_QTY NUMBER, --현재고
SAFE_QTY NUMBER --안전재고
)
--현재고<안전재고 인부품:A0,B0,C0
INSERT INTO PART_MST VALUES('A0','A0',5,10);
INSERT INTO PART_MST VALUES('B0','B0',5,10);
INSERT INTO PART_MST VALUES('C0','C0',7,10);
INSERT INTO PART_MST VALUES('D0','D0',15,10);
CREATE TABLE RPART_MST( --대체부품 테이블
RPART_CD VARCHAR2(10) CONSTRAINT RPART_MST_PK PRIMARY KEY,
RPART_NM VARCHAR2(10),
OPART_CD VARCHAR2(10), --원부품코드
CURR_QTY NUMBER, --현재고
SAFE_QTY NUMBER, --안전재고
ORDERING NUMBER --우선순위
)
CREATE INDEX ORDER_IDX ON SCOTT.RPART_MST(ORDERING)
CREATE INDEX OPART_IDX ON SCOTT.RPART_MST(OPART_CD)
--현재고>=안전재고인 부품:A3,A4,B2,B3,D1
INSERT INTO RPART_MST VALUES('A1','A1','A0',5,10,1);
INSERT INTO RPART_MST VALUES('A2','A2','A0',7,10,2);
INSERT INTO RPART_MST VALUES('A3','A3','A0',10,10,3);
INSERT INTO RPART_MST VALUES('A4','A4','A0',15,10,4);
INSERT INTO RPART_MST VALUES('B1','B1','B0',15,10,1);
INSERT INTO RPART_MST VALUES('B2','B2','B0',17,10,2);
INSERT INTO RPART_MST VALUES('B3','B3','B0',10,10,3);
INSERT INTO RPART_MST VALUES('C1','C1','C0',5,10,1);
INSERT INTO RPART_MST VALUES('C2','C2','C0',1,10,2);
INSERT INTO RPART_MST VALUES('C3','C3','C0',3,10,3);
INSERT INTO RPART_MST VALUES('D1','D1','D0',13,10,1);
ANALYZE TABLE PART_MST COMPUTE STATISTICS FOR ALL INDEXED COLUMNS
ANALYZE TABLE RPART_MST COMPUTE STATISTICS FOR ALL INDEXED COLUMNS
SELECT
A.PART_cd,A.CURR_QTY,A.SAFE_QTY
,B.RPART_CD,B.CURR_QTY,B.SAFE_QTY,B.ORDERING
FROM
(
SELECT
RPART_CD,RPART_NM,OPART_CD,CURR_QTY,SAFE_QTY,ORDERING
,ROW_NUMBER() OVER(PARTITION BY OPART_CD ORDER BY ORDERING) RM
FROM RPART_MST
WHERE CURR_QTY>=SAFE_QTY
) B,PART_MST A
WHERE A.CURR_QTY<A.SAFE_QTY
AND A.PART_CD=B.OPART_CD
AND A.PART_CD>' '
AND RM=1
Operation Object Name Rows Bytes Cost Object Node In/Out PStart PStop
SELECT STATEMENT Optimizer Mode=CHOOSE 4 7
HASH JOIN 4 288 7
TABLE ACCESS BY INDEX ROWID SCOTT.PART_MS 2 12 2
INDEX RANGE SCAN SCOTT.PART_MST_PK 4 1
VIEW 6 396 4
WINDOW SORT PUSHED RANK 6 60 4
TABLE ACCESS BY INDEX ROWID SCOTT.RPART_MST 6 60 2
INDEX RANGE SCAN SCOTT.OPART_IDX 11 1
CREATE TABLE PART(
PART_CD VARCHAR2(10) CONSTRAINT PART_PK PRIMARY KEY,
PART_NM VARCHAR2(10),
REPL_PART VARCHAR2(10),
CURR_QTY NUMBER,
SAFE_QTY NUMBER
)
INSERT INTO PART VALUES('A0','A0','A1',1,10);
INSERT INTO PART VALUES('A1','A1','A2',3,10);
INSERT INTO PART VALUES('A2','A2','A3',5,10);
INSERT INTO PART VALUES('A3','A3','A4',1,10);
INSERT INTO PART VALUES('A4','A4',NULL,0,10);
INSERT INTO PART VALUES('B0','B0','B1',1,10);
INSERT INTO PART VALUES('B1','B1','B2',4,10);
INSERT INTO PART VALUES('B2','B2',NULL,0,10);
INSERT INTO PART VALUES('C0','C0','C1',1,10);
INSERT INTO PART VALUES('C1','C1','C2',4,10);
INSERT INTO PART VALUES('C2','C2',NULL,0,10);
INSERT INTO PART VALUES('D0','D0','D1',10,10);
INSERT INTO PART VALUES('D1','D1',NULL,4,10);
CREATE TABLE PORDER(
PART_CD VARCHAR2(10) CONSTRAINT PORDER_PK PRIMARY KEY,
ORD_QTY NUMBER
)
INSERT INTO PORDER VALUES('A0',6); //A0계열:총 10개
INSERT INTO PORDER VALUES('B0',5); //B0계열:총 5개
INSERT INTO PORDER VALUES('C0',7); //C0계열:총 4개
SELECT
ORD_PART "주문부품"
,ORD_QTY "주문수량"
,OUT_PART "출고부품"
,DECODE(LEAST(ACCUM_QTY,ORD_QTY),ACCUM_QTY,CURR_QTY,ORD_QTY-LAG(ACCUM_QTY,1) OVER(PARTITION BY GUBUN ORDER BY LV)) "출고수량"
,DECODE(SIGN(TOT_QTY-ORD_QTY),-1,'<FONT COLOR=RED>부품부족('||(TOT_QTY-ORD_QTY)||')</FONT>','출고가능') "상태"
FROM
(
SELECT LV
,B.PART_CD ORD_PART
,A.PART_CD OUT_PART
,GUBUN
,CURR_QTY
,ACCUM_QTY,ORD_QTY
,SUM(DECODE(SIGN(ACCUM_QTY-B.ORD_QTY),0,1,1,1,0)) OVER( PARTITION BY GUBUN ORDER BY LV) OK_FLAG
,TOT_QTY
FROM
(
SELECT
LEVEL LV
,PART_CD
,REPL_PART
,SUBSTR(PART_CD,1,1) GUBUN
,CURR_QTY
,SUM(CURR_QTY) OVER(PARTITION BY SUBSTR(PART_CD,1,1) ORDER BY LEVEL) ACCUM_QTY
,SUM(CURR_QTY) OVER(PARTITION BY SUBSTR(PART_CD,1,1)) TOT_QTY
FROM PART
CONNECT BY PRIOR REPL_PART=PART_CD
START WITH PART_CD IN(SELECT PART_CD FROM PORDER)
) A,PORDER B
WHERE A.PART_CD LIKE SUBSTR(B.PART_CD,1,1)||'%'
)
WHERE OK_FLAG<=1
주문부품 주문수량 출고부품 출고수량 상태
-------------------------------------------
A0 6 A0 1 출고가능
A0 6 A1 3 출고가능
A0 6 A2 2 출고가능
B0 5 B0 1 출고가능
B0 5 B1 4 출고가능
C0 7 C0 0 <FONT COLOR=RED>부품부족(-3)</FONT>
C0 7 C1 4 <FONT COLOR=RED>부품부족(-3)</FONT>
C0 7 C2 0 <FONT COLOR=RED>부품부족(-3)</FONT>
- 강좌 URL : http://www.gurubee.net/lecture/2488
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.