좀더쉽게할수없을까요?(2) 저번에 질문드린건데 데이터가조금변경돼었습니다 ... 0 3 2,488

by 김용한 [SQL Query] [2011.07.13 23:47:45]


※. 아래 쿼리질문과 틀린점은 월계획테이블과, 일계획테이블의 모델코드값의 비율이 M:N 이라는점입니다

즉  아래질문의 쿼리는( M:N => M개수> N개수 ) 가 항상성립하여서 M=N(+) 조인이 성립이돼었는데
지금쿼리는 ( M:N => M>N 일지 M<N일지 정해지지않은겁니다) 그래서 UNION ALL로  SUM해서
  채워 넣은겁니다
  코드  테이블의값은 물론 두테이블의 결과테이블과 OUTER JOIN 해서 집어넣으면돼는거구요...
아래의 제가한쿼리와 다른방법이있는지 질문드립니다 ^^

=> 일기준코드테이블에 요 ROW값이 아래 쿼리질문과 다른점입니다 요것때문에 M:N이 M<N이 돼는 조건입니다.
3122 5.34HC HC 0 0 444


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 3.96GE GE 0 0 321
3200 3.96GL GL 0 0 532
3250 3.96GE GE 0 0 346
3260 3.96GE GE 0 0 876
3122 5.34HC HC 0 0 444


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
3122 5.34HC HC 0 0 444
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
3122 5.34HC HC 0 444
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, '3.96GE' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 321 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '3.96GL' MODELCODE2, 'GL' MODELCODE, 0 SILJUK, 0 D_PLAN, 532 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 346 D_SILJUK FROM DUAL UNION ALL
SELECT '3260' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 876 D_SILJUK FROM DUAL UNION ALL
SELECT '3122' OPERATIONNAMECODE, '5.34HC' MODELCODE2, 'HC' MODELCODE, 0 SILJUK, 0 D_PLAN, 444 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.14 00:42:20]
3개 다 한번에 유니온 하세요

SELECT operationnamecode
, MAX(modelcode2) modelcode2
, modelcode
, SUM(siljuk) siljuk
, SUM(d_plan) d_plan
, SUM(d_siljuk) d_siljuk
FROM
(
SELECT * FROM t1
UNION ALL
SELECT * FROM t2
UNION ALL
SELECT operationnamecode
, '' modelcode2
, modelcode
, 0 siljuk
, attr2 d_plan
, 0 d_siljuk
FROM t3
)
GROUP BY operationnamecode, modelcode
ORDER BY operationnamecode, modelcode
;

by 김용한 [2011.07.14 06:14:36]
아 감사합니다 ^^

by 김용한 [2011.07.14 06:36:56]
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, '3.96GE' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 321 D_SILJUK FROM DUAL UNION ALL
SELECT '3200' OPERATIONNAMECODE, '3.96GL' MODELCODE2, 'GL' MODELCODE, 0 SILJUK, 0 D_PLAN, 532 D_SILJUK FROM DUAL UNION ALL
SELECT '3250' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 346 D_SILJUK FROM DUAL UNION ALL
SELECT '3260' OPERATIONNAMECODE, '3.96GE' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 0 D_PLAN, 876 D_SILJUK FROM DUAL UNION ALL
SELECT '3122' OPERATIONNAMECODE, '5.34HC' MODELCODE2, 'HC' MODELCODE, 0 SILJUK, 0 D_PLAN, 444 D_SILJUK FROM DUAL
)
UNION ALL
SELECT * FROM
(
SELECT '3122' OPERATIONNAMECODE, '' MODELCODE2, 'GE' MODELCODE, 0 SILJUK, 2022 ATTR2, 0 D_SILJUK FROM DUAL UNION ALL
SELECT '3200', '', 'GL', 0, 3344, 0 FROM DUAL UNION ALL
SELECT '3250', '', 'GL', 0, 4487, 0 FROM DUAL
)
)
GROUP BY OPERATIONNAMECODE, MODELCODE
ORDER BY OPERATIONNAMECODE, MODELCODE

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