Накидал вот тут две функции:
Код:
CREATE OR REPLACE FUNCTION RemainsAtDate (
s_article IN SMCard.article%TYPE,
s_date IN SMDocuments.createdat%TYPE,
s_storeloc IN SMStoreLocations.id%TYPE
) RETURN SMSpec.quantity%TYPE
IS
o_quantity SMSpec.quantity%TYPE;
BEGIN
SELECT SUM(q)
INTO o_quantity
FROM (
(SELECT SMSpec.quantity AS q FROM SMSpec, SMDocuments
WHERE
SMSpec.doctype = SMDocuments.doctype AND
SMSpec.docid = SMDocuments.id AND
SMDocuments.locationto = s_storeloc AND
SMSpec.article = s_article AND
SMDocuments.docstate = 3 AND
SMDocuments.createdat <= s_date
)
UNION ALL
(SELECT (0 - SMSpec.quantity) AS q FROM SMSpec, SMDocuments
WHERE
SMSpec.doctype = SMDocuments.doctype AND
SMSpec.docid = SMDocuments.id AND
SMDocuments.locationfrom = s_storeloc AND
SMSpec.article = s_article AND
SMDocuments.docstate = 3 AND
SMDocuments.createdat <= s_date));
return o_quantity;
END;
/
Код:
CREATE OR REPLACE FUNCTION RemainsAtDate2 (
s_article IN SMCard.article%TYPE,
s_date IN SMDocuments.createdat%TYPE,
s_storeloc IN SMStoreLocations.id%TYPE
) RETURN SMSpec.quantity%TYPE
IS
o_quantity SMSpec.quantity%TYPE;
BEGIN
SELECT
(SELECT SUM(SMSpec.quantity) FROM SMSpec, SMDocuments
WHERE
SMSpec.doctype = SMDocuments.doctype AND
SMSpec.docid = SMDocuments.id AND
SMDocuments.locationto = s_storeloc AND
SMSpec.article = s_article AND
SMDocuments.docstate = 3 AND
SMDocuments.createdat <= s_date
)
-
(SELECT SUM(SMSpec.quantity) FROM SMSpec, SMDocuments
WHERE
SMSpec.doctype = SMDocuments.doctype AND
SMSpec.docid = SMDocuments.id AND
SMDocuments.locationfrom = s_storeloc AND
SMSpec.article = s_article AND
SMDocuments.docstate = 3 AND
SMDocuments.createdat <= s_date)
INTO o_quantity FROM DUAL;
return o_quantity;
END;
/
Считаю сумму остатков в месте хранения на какую-нибудь дату: вторая функция выдаёт количество больше чем первая. Не вижу ошибки, помогите найти?