Давай мы не будем друг друга мучить.
Код:
SELECT StoreLoc,
Article,
IncomeId,
IncomeType,
IncomeSpecItem,
max(GoodsOwner) GoodsOwner,
max(IncomeClientIndex) ClientIndex,
max(IncomeVatRate) VatRate,
max(IncomeQ) DocQuantity,
max(IncomeSum) DocSum,
max(IncomeNoVat) DocSumNoVat,
ForcedMapping,
sum(Quantity) Quantity
FROM (
SELECT /*+ ORDERED USE_NL(S,A) FULL(A) FULL(M.U_MapRep.FFMapRep) */
SaleLocationTo StoreLoc,
Article,
IncomeId,
IncomeType,
IncomeSpecItem,
GoodsOwner,
IncomeClientIndex,
IncomeVatRate,
IncomeQ,
IncomeSum,
IncomeNoVat,
ForcedMapping,
Quantity
FROM FVMapRep
WHERE SaleDate between :date_start and :date_end
and forcedMapping=0
and SaleLocationTo is not NULL
UNION ALL SELECT /*+ ORDERED USE_NL(S,A) FULL(A) FULL(M.U_MapRep.FFMapRep) */
SaleLocationFrom StoreLoc,
Article,
IncomeId,
IncomeType,
IncomeSpecItem,
GoodsOwner,
IncomeClientIndex,
IncomeVatRate,
IncomeQ,
IncomeSum,
IncomeNoVat,
ForcedMapping,
-Quantity
FROM FVMapRep
WHERE SaleDate between :date_start and :date_end
and forcedMapping=0
and SaleLocationFrom is not NULL )
GROUP BY StoreLoc,
Article,
IncomeId,
IncomeType,
IncomeSpecItem,
ForcedMapping
HAVING ROUND(sum(Quantity),3) <> 0
Это запрос по расчету остатков, как он есть в См2000.
Учти, что Date_start - это дата начала товародвижения в базе.