ÀÌ ±Û¿¡ ´ëÇÑ ´ñ±ÛÀÌ ÃÑ 3°Ç ÀÖ½À´Ï´Ù.°á°úÅ×À̺í TC ¸¦ »ç¿ëÇÒ °æ¿ì
/* ÄÚµå/ÀÏÀÚº° Ãâ°í·® ÃßÃâ */
for c1 in ( select code, reqdate, reqqty
from ta
order by code, reqdate ) loop
/* ÃßÃâÇÑ Ãâ°í·® */
wk_reqqty := c1.reqqty;
/* ÃßÃâÇÑ ÄÚµåÀÇ Ãâ°íÀÏÀÚ ÀÌÀü Ãâ°í·® °è»ê */
select sum(reqqty) into wk_preqty
from ta
where code = c1.code
and reqdate < c1.reqdate;
/* ÃßÃâÇÑ Äڵ忡 ´ëÇÑ ÀÔ°í·® ÃßÃâ */
for c2 in ( select code, inputdate, inventory
from tb
where code = c1.code
order by inputdate ) loop
/* ÃßÃâÇÑ ÀÔ°í·® */
wk_inventory := c2.inventory;
/* ÀÌÀü Ãâ°í·®°ú ÀÔ°í·® ºñ±³ */
if (wk_preqty > 0) then
if (wk_preqty >= wk_inventory) then
wk_preqty := wk_preqty - wk_inventory;
wk_inventory := 0;
else
wk_inventory := wk_inventory - wk_preqty;
wk_preqty := 0;
end if;
end if;
/* ÀÌÀü Ãâ°í·® ¹Ý¿µ ÈÄ ÀÔ°í·®ÀÌ ³²Àº °æ¿ì Ãâ°í·® ¹Ý¿µ */
if (wk_inventory > 0) then
if (wk_inventory >= wk_reqqty) then
insert into tc (reqdate, code, reqqty, inputdate, outqty)
values (c1.reqdate, c1.code, c1.reqqty, c2.inputdate, wk_reqqty);
exit;
else
insert into tc (reqdate, code, reqqty, inputdate, outqty)
values (c1.reqdate, c1.code, c1.reqqty, c2.inputdate, wk_inventory);
wk_reqqty := wk_reqqty - wk_inventory;
end if;
end if;
end loop; /* end of for c2 */
end loop; /* end of for c1 */
select * from tc
order by code, reqdate;
ÀÛ¾÷¿ë Å×À̺íÀ» »ç¿ëÇÏÁö ¾Ê°í ÇϳªÀÇ Äõ¸®·Î ÇØ°áÇÏ±æ ¿øÇϽŰǰ¡¿ä ?
´ÔÀÇ Äõ¸®µµ ¿Ã·ÁÁÖ¼¼¿ä ^^
ÀÓ¸í¼ø(a3sd)´ÔÀÌ 2005-10-17 20:20:49¿¡ ÀÛ¼ºÇÑ ´ñ±ÛÀÔ´Ï´Ù.
ÀÌ ´ñ±ÛÀº 2005-10-17 20:22:30¿¡ ¸¶Áö¸·À¸·Î ¼öÁ¤µÇ¾ú½À´Ï´Ù.SELECT a.reqdate, a.code, a.reqqty, b.inputdate,
(CASE
WHEN a.bef >= b.bef AND a.bef < b.aft AND a.aft > b.aft
THEN b.aft - a.bef
WHEN a.aft > b.bef AND a.aft <= b.aft AND a.bef < b.bef
THEN a.aft - b.bef
WHEN a.bef < b.bef AND a.aft > b.aft
THEN b.inventory
WHEN a.bef >= b.bef AND a.aft <= b.aft
THEN a.reqqty
END
) outqty
FROM (SELECT reqdate, code, reqqty,
NVL
(SUM (reqqty) OVER (PARTITION BY code ORDER BY code,
reqdate ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),
0
) bef,
SUM (reqqty) OVER (PARTITION BY code ORDER BY code,
reqdate) aft
FROM ta) a,
(SELECT inputdate, code, inventory,
NVL
(SUM (inventory) OVER (PARTITION BY code ORDER BY code,
inputdate ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),
0
) bef,
SUM (inventory) OVER (PARTITION BY code ORDER BY code,
inputdate) aft
FROM tb) b
WHERE a.code = b.code
AND ( a.bef >= b.bef AND a.bef < b.aft AND a.aft > b.aft
OR a.aft > b.bef AND a.aft <= b.aft AND a.bef < b.bef
OR a.bef < b.bef AND a.aft > b.aft
OR a.bef >= b.bef AND a.aft <= b.aft
)
ORDER BY a.code, a.reqdate, b.inputdate
-- SQL Äõ¸® Áú¹®Àº SQL ±îÆä¿¡¼... http://cafe.daum.net/oraclesqltuning
|
|
|