INSERT /*+ APPEND NOLOGGING */
INTO TTRemIncome
(StoreLoc,
Article,
DocId,
DocType,
SpecItem,
GoodsOwner,
ClientIndex,
VATRate,
DocQuantity,
DocSum,
DocSumNoVAT,
Forced,
Quantity)
(SELECT StoreLoc,
Article,
IncomeId,
IncomeType,
IncomeSpecItem,
min(GoodsOwner) KEEP(DENSE_RANK FIRST ORDER BY Range) GoodsOwner,
min(IncomeClientIndex) KEEP(DENSE_RANK FIRST ORDER BY Range) ClientIndex,
min(IncomeVatRate) KEEP(DENSE_RANK FIRST ORDER BY Range) VatRate,
min(IncomeQ) KEEP(DENSE_RANK FIRST ORDER BY Range) DocQuantity,
min(IncomeSum) KEEP(DENSE_RANK FIRST ORDER BY Range) DocSum,
min(IncomeNoVat) KEEP(DENSE_RANK FIRST ORDER BY Range) DocSumNoVat,
ForcedMapping,
sum(Quantity) Quantity
FROM (SELECT decode(SaleLocationTo, -2, to_number(null), SaleLocationTo) StoreLoc,
Article,
IncomeId,
IncomeType,
decode(IncomeSpecItem, -1, to_number(null), IncomeSpecItem) IncomeSpecItem,
GoodsOwner,
decode(IncomeClientIndex,
0,
to_number(null),
IncomeClientIndex) IncomeClientIndex,
nvl(IncomeVATRate, 0) IncomeVatRate,
nvl(IncomeQ, 0) IncomeQ,
nvl(IncomeSum, 0) IncomeSum,
nvl(IncomeNoVAT, 0) IncomeNoVat,
nvl(ForcedMapping, 0) ForcedMapping,
Quantity,
1 Range
FROM FFMapRep
WHERE decode(SaleLocationTo, -2, to_number(null), SaleLocationTo) is not NULL
and SaleDate between to_date('01.01.2012', 'DD.MM.YYYY') and
to_date('25.11.2020', 'DD.MM.YYYY')
UNION ALL
SELECT decode(SaleLocationFrom,
-2,
to_number(null),
SaleLocationFrom) StoreLoc,
Article,
IncomeId,
IncomeType,
decode(IncomeSpecItem, -1, to_number(null), IncomeSpecItem) IncomeSpecItem,
GoodsOwner,
decode(IncomeClientIndex,
0,
to_number(null),
IncomeClientIndex) IncomeClientIndex,
nvl(IncomeVATRate, 0) IncomeVatRate,
nvl(IncomeQ, 0) IncomeQ,
nvl(IncomeSum, 0) IncomeSum,
nvl(IncomeNoVAT, 0) IncomeNoVat,
nvl(ForcedMapping, 0) ForcedMapping,
-Quantity,
1 Range
FROM FFMapRep
WHERE decode(SaleLocationFrom,
-2,
to_number(null),
SaleLocationFrom) is not NULL
and SaleDate between to_date('01.01.2012', 'DD.MM.YYYY') and
to_date('25.11.2020', 'DD.MM.YYYY')
UNION ALL
SELECT M.SaleLocationTo StoreLoc,
M.Article,
M.IncomeId,
M.IncomeType,
decode(M.IncomeSpecItem,
-1,
to_number(null),
M.IncomeSpecItem) IncomeSpecItem,
M.GoodsOwner,
decode(M.IncomeClientIndex,
0,
to_number(null),
M.IncomeClientIndex) IncomeClientIndex,
nvl(M.IncomeVATRate, 0) IncomeVatRate,
nvl(M.IncomeQ, 0) IncomeQ,
nvl(M.IncomeSum, 0) IncomeSum,
nvl(M.IncomeNoVAT, 0) IncomeNoVat,
'0' ForcedMapping,
M.Quantity,
3 Range
FROM FFMapRep_ M
WHERE M.SaleDate = to_date('31.12.2011', 'DD.MM.YYYY')
and M.SaleType = 'WI'
and exists (select *
from TTRemDocList TD
where TD.DocType = M.SaleType
and TD.ID = M.SaleId)
UNION ALL
SELECT StoreLoc,
Article,
to_char(NULL) IncomeId,
to_char(NULL) IncomeType,
to_number(NULL) IncomeSpecItem,
0 GoodsOwner,
to_number(NULL) IncomeClientIndex,
0 IncomeVatRate,
0 IncomeQ,
0 IncomeSum,
0 IncomeNoVat,
'1' ForcedMapping,
SUM(Quantity) Quantity,
3 Range
FROM FFRemains_
WHERE RemDate = to_date('31.12.2011', 'DD.MM.YYYY')
GROUP BY StoreLoc, Article
HAVING SUM(Quantity) < 0)
GROUP BY StoreLoc,
Article,
IncomeId,
IncomeType,
IncomeSpecItem,
ForcedMapping
HAVING ROUND(sum(Quantity), 3) <> 0 or (IncomeType = 'BR' and (ROUND(SUM(IncomeNoVat), 4) <> 0 or ROUND(SUM(IncomeSum), 4) <> 0)))