INSERT INTO TTCARDCHANGES
(ARTICLE, REMOVED, PRICE, DEPID, IDCLASS, IDSCALE, DATATYPE)
SELECT ARTICLE,
DECODE(PRICE, 0, '1', REMOVED),
DECODE(REMOVED, '1', 0, PRICE),
0,
IDCLASS,
DECODE(BITAND(FLAGS, 2), 0, IDSCALE, NULL),
DATATYPE
FROM (SELECT C.ARTICLE,
C.IDCLASS,
C.FLAGS,
C.IDSCALE,
C.DATATYPE,
DECODE(C.CASHLOAD,
'0',
'1',
DECODE(C.ACCEPTED, 1, '0', '1')) REMOVED,
NVL(P.PRICE, 0) PRICE
FROM SMCARD C, SMPRICES P
WHERE /*ROWNUM <= :b1 + 1
AND */C.ACCEPTED IN (1, 2)
--AND BITAND((POWER(2, C.DATATYPE)), :b2) != 0
AND P.ARTICLE(+) = C.ARTICLE
AND P.STORELOC(+) = 48
AND P.PRICETYPE(+) = 36
/*
AND EXISTS
(SELECT *
FROM SACARDCLASS A, TTCASHCLASS T
WHERE A.ID = C.IDCLASS
AND A.TREE LIKE
(DECODE(T.TREE, '#', '%', T.TREE || '%'))
)
*/
AND (EXISTS (SELECT *
FROM SMCARDSECURITYLOG L
WHERE C.ARTICLE = L.ARTICLE
AND L.EVENTTIME >= to_date('10.09.2016', 'dd.mm.yyyy')) OR EXISTS
(SELECT *
FROM SMPRICEHISTORY L
WHERE C.ARTICLE = L.ARTICLE
AND L.PRICETYPE = 36
AND L.STORELOC = 48
AND L.EVENTTIME >= to_date('10.09.2016', 'dd.mm.yyyy')) OR EXISTS
(SELECT *
FROM SMSTOREUNITHIST L
WHERE L.ARTICLE = C.ARTICLE
AND L.EVENTTIME >= to_date('10.09.2016', 'dd.mm.yyyy')) OR EXISTS
(SELECT *
FROM SMDISCQUANTITYLOG L
WHERE L.ARTICLE = C.ARTICLE
AND L.PRICEID = 36
AND L.EVENTTIME >= to_date('10.09.2016', 'dd.mm.yyyy'))))