Следующим запросом нахожу расходные накладные. Код операции у всех 1. Продажа. Просветите меня пожалуйста, как теперь учесть только возвраты по документам?
Код:
SELECT SACARDCLASS_TREE as "Код узла",
SACARDCLASS_NAME as "Классификатор",
SMCARD_ARTICLE as "Артикул",
SMCARD_NAME as "Полное наименование",
SUM(SMSPEC_QUANTITY) as "Количество",
SUM(SMSPEC_TOTALPRICE) as "Сумма_реализации",
round(SUM(SMSPEC_QUANTITY)/(to_date('30.06.2007', 'DD.MM.YYYY') - to_date('01.04.2007', 'DD.MM.YYYY') + 1), 3) as "Ср сут реализация_Количество",
round(SUM(SMSPEC_TOTALPRICE)/(to_date('30.06.2007', 'DD.MM.YYYY') - to_date('01.04.2007', 'DD.MM.YYYY') + 1), 2) as "Ср сут реализация_Сумма",
STORELOCATION as "Место хранения",
SMDOCUMENTS_OPCODE as "Код операции",
SMDOCUMENTS_ID as "Номер документа"
FROM (
SELECT SACARDCLASS.TREE SACARDCLASS_TREE,
SACARDCLASS.NAME SACARDCLASS_NAME,
SMCARD.ARTICLE SMCARD_ARTICLE,
SMCARD.NAME SMCARD_NAME,
SMSPEC.QUANTITY SMSPEC_QUANTITY,
SMDOCUMENTS.ID SMDOCUMENTS_ID,
SMSPEC.TOTALPRICE SMSPEC_TOTALPRICE,
SMDOCUMENTS.CREATEDAT SMDOCUMENTS_CREATEDAT,
NVL(SMDOCUMENTS.LOCATIONTO, SMDOCUMENTS.LOCATIONFROM) STORELOCATION,
SMDOCUMENTS.OPCODE SMDOCUMENTS_OPCODE
FROM SACARDCLASS,
SMCARD,
SMSPEC,
SMDOCUMENTS
WHERE SACARDCLASS.ID = SMCARD.IDCLASS
AND SMSPEC.DOCTYPE = SMDOCUMENTS.DOCTYPE
AND SMSPEC.DOCID = SMDOCUMENTS.ID
AND SMSPEC.ARTICLE = SMCARD.ARTICLE
AND SMDOCUMENTS.DOCSTATE = 3
AND SMSPEC.Article IN(
SELECT SMCARD.ARTICLE
FROM SMCARD,
SACARDCLASS
WHERE SMCARD.IDCLASS = SACARDCLASS.ID
AND SACARDCLASS.TREE LIKE '16.%'
)
AND SMDOCUMENTS.CREATEDAT >= to_date('01.04.2007', 'DD.MM.YYYY')
AND SMDOCUMENTS.CREATEDAT <= to_date('30.06.2007', 'DD.MM.YYYY')
AND NVL(SMDOCUMENTS.LOCATIONTO, SMDOCUMENTS.LOCATIONFROM) IN(22)
AND SMDOCUMENTS.DOCTYPE IN('WO')
ORDER BY SMCARD.ARTICLE
)
GROUP BY SACARDCLASS_TREE,
SACARDCLASS_NAME,
SMCARD_ARTICLE,
SMCARD_NAME,
STORELOCATION,
SMDOCUMENTS_OPCODE,
SMDOCUMENTS_ID
ORDER BY SMDOCUMENTS_ID ASC