좀더쉽게할수있을까요? 0 2 2,476

by 김용한 [Oracle 기초] [2011.07.12 21:53:09]


OPERATIONNAMECODE MODELCODE2 MODELCODE SILJUK D_PLAN D_SILJUK
3122 3.96GE GE 100 0 0
3122 3.96GL GL 200 0 0
3122 4.97GQ GQ 300 0 0
3200 3.96GE GE 400 0 0
3200 3.96GL GL 500 0 0
3200 5.84HK HK 900 0 0
3200 2.34CT CT 750 0 0
3250 3.96GE GE 580 0 0
3250 3.96GL GL 690 0 0
3250 5.48HQ HQ 770 0 0
3250 7.34HL HL 290 0 0
3250 6.54TC TC 334 0 0
3250 6.54TC TC 334 0 0
3260 3.96GE GE 223 0 0
3260 4.97GL GL 436 0 0

1). 월기준코드(위)


OPERATIONNAMECODE MODELCODE2 MODELCODE SILJUK D_PLAN D_SILJUK
3122 GE 0 0 321
3200 GL 0 0 532
3250 GE 0 0 346
3260 GE 0 0 876

2). 일기준코드(위)

OPERATIONNAMECODE MODELCODE2 MODELCODE SILJUK D_PLAN D_SILJUK
3122 3.96GE GE 100 0 321
3122 3.96GL GL 200 0 0
3122 4.97GQ GQ 300 0 0
3200 2.34CT CT 750 0 0
3200 3.96GE GE 400 0 0
3200 3.96GL GL 500 0 532
3200 5.84HK HK 900 0 0
3250 3.96GE GE 580 0 346
3250 3.96GL GL 690 0 0
3250 7.34HL HL 290 0 0
3250 5.48HQ HQ 770 0 0
3250 6.54TC TC 668 0 0
3260 3.96GE GE 223 0 876
3260 4.97GL GL 436 0 0

3). OPERATIONNAMECODE 와 MODELCODE 기준 합침(위)
    - D_PLAN(일 계획값) 은 코드테이블에서가져오기위해서 0으로 했음)

OPERATIONNAMECODE MODELCODE ATTR2
3122 GE 2022
3200 GL 3344
3250 GL 4487


4). 코드테이블(위)
  - ATTR2는 일계획값

OPERATIONNAMECODE MODELCODE2 MODELCODE SILJUK ATTR2 D_SILJUK
3122 3.96GE GE 100 2022 321
3122 3.96GL GL 200 0
3122 4.97GQ GQ 300 0
3200 2.34CT CT 750 0
3200 3.96GE GE 400 0
3200 3.96GL GL 500 3344 532
3200 5.84HK HK 900 0
3250 3.96GE GE 580 346
3250 3.96GL GL 690 4487 0
3250 5.48HQ HQ 770 0
3250 6.54TC TC 668 0
3250 7.34HL HL 290 0
3260 3.96GE GE 223 876
3260 4.97GL GL 436 0


5) . 3) 과 4) 두테이블을 이용하여 3)에 D_PLAN(일계획값을 채워넣음) (위)

WITH T AS
(
SELECT OPERATIONNAMECODE, MAX(MODELCODE2) MODELCODE2, MODELCODE
, SUM(SILJUK) SILJUK
, SUM(D_PLAN) D_PLAN
, SUM(D_SILJUK) D_SILJUK
  FROM
(
SELECT * FROM
(
SELECT '3122' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 100 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3122' OPERATIONNAMECODE, '3.96GL' MODELCODE2, 'GL' MODELCODE, 200 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3122' OPERATIONNAMECODE, '4.97GQ' MODELCODE2, 'GQ' MODELCODE, 300 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 400 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '3.96GL' MODELCODE2, 'GL' MODELCODE, 500 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '5.84HK' MODELCODE2, 'HK' MODELCODE, 900 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '2.34CT' MODELCODE2, 'CT' MODELCODE, 750 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 580 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '3.96GL' MODELCODE2, 'GL' MODELCODE, 690 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '5.48HQ' MODELCODE2, 'HQ' MODELCODE, 770 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '7.34HL' MODELCODE2, 'HL' MODELCODE, 290 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '6.54TC' MODELCODE2, 'TC' MODELCODE, 334 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '6.54TC' MODELCODE2, 'TC' MODELCODE, 334 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3260' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 223 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3260' OPERATIONNAMECODE, '4.97GL' MODELCODE2, 'GL' MODELCODE, 436 SILJUK, 0 D_PLAN, 0 D_SILJUK FROM DUAL
)
UNION ALL
SELECT * FROM
(
SELECT '3122' OPERATIONNAMECODE, '' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 321 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '' MODELCODE2, 'GL' MODELCODE, 0 SILJUK, 0 D_PLAN, 532 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 346 D_SILJUK FROM DUAL UNION ALL
SELECT '3260' OPERATIONNAMECODE, '' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 876 D_SILJUK FROM DUAL
)
)
GROUP BY OPERATIONNAMECODE, MODELCODE
ORDER BY OPERATIONNAMECODE, MODELCODE
)
SELECT T.OPERATIONNAMECODE, T.MODELCODE2, T.MODELCODE, T.SILJUK, S.ATTR2, T.D_SILJUK
  FROM T, (
  SELECT '3122' OPERATIONNAMECODE, 'GE' MODELCODE, 2022 ATTR2 FROM DUAL UNION ALL
  SELECT '3200', 'GL', 3344 FROM DUAL UNION ALL
  SELECT '3250', 'GL', 4487 FROM DUAL
  )S
WHERE T.OPERATIONNAMECODE = S.OPERATIONNAMECODE(+)
  AND T.MODELCODE = S.MODELCODE(+)
ORDER BY T.OPERATIONNAMECODE, MODELCODE2  


좀더 쉽게할수없을까요? ;;
by 마농 [2011.07.12 22:56:26]
WITH t1 AS
(
SELECT '3122' operationnamecode, '3.96GE' modelcode2, 'GE' modelcode, 100 siljuk FROM dual
UNION ALL SELECT '3122', '3.96GL', 'GL', 200 FROM dual
UNION ALL SELECT '3122', '4.97GQ', 'GQ', 300 FROM dual
UNION ALL SELECT '3200', '3.96GE', 'GE', 400 FROM dual
UNION ALL SELECT '3200', '3.96GL', 'GL', 500 FROM dual
UNION ALL SELECT '3200', '5.84HK', 'HK', 900 FROM dual
UNION ALL SELECT '3200', '2.34CT', 'CT', 750 FROM dual
UNION ALL SELECT '3250', '3.96GE', 'GE', 580 FROM dual
UNION ALL SELECT '3250', '3.96GL', 'GL', 690 FROM dual
UNION ALL SELECT '3250', '5.48HQ', 'HQ', 770 FROM dual
UNION ALL SELECT '3250', '7.34HL', 'HL', 290 FROM dual
UNION ALL SELECT '3250', '6.54TC', 'TC', 334 FROM dual
UNION ALL SELECT '3260', '3.96GE', 'GE', 223 FROM dual
UNION ALL SELECT '3260', '4.97GL', 'GL', 436 FROM dual
)
, t2 AS
(
SELECT '3122' operationnamecode, 'GE' modelcode, 321 d_siljuk FROM dual
UNION ALL SELECT '3200', 'GL', 532 FROM dual
UNION ALL SELECT '3250', 'GE', 346 FROM dual
UNION ALL SELECT '3260', 'GE', 876 FROM dual
)
, t3 AS
(
SELECT '3122' operationnamecode, 'GE' modelcode, 2022 attr2 FROM dual
UNION ALL SELECT '3200', 'GL', 3344 FROM dual
UNION ALL SELECT '3250', 'GL', 4487 FROM dual
)
SELECT t1.operationnamecode
, t1.modelcode2
, t1.modelcode
, t1.siljuk
, t3.attr2
, t2.d_siljuk
FROM t1, t2, t3
WHERE t1.operationnamecode = t2.operationnamecode(+)
AND t1.operationnamecode = t3.operationnamecode(+)
AND t1.modelcode = t2.modelcode(+)
AND t1.modelcode = t3.modelcode(+)
ORDER BY t1.operationnamecode, t1.modelcode2
;

by 김용한 [2011.07.12 23:12:10]
감사합니다 ^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입