Добрый день,
Есть задача выбрать продажи за несколько периодов для отправки их в другую БД.
Написал запрос, выбирает то что нужно. Но работает по 10 часов :(
Подскажите может я что то не так делаю.
Код:
SELECT /*+ FIRST_ROWS(10) */
C.ARTICLE,
-- Продажи
nvl((SELECT
SUM((CASE WHEN F.SALEOP=3 THEN (-1)*F.SALEQ ELSE F.SALEQ END)) AS SALEQ
FROM SUPERMAG.FFMAPREP F
WHERE
F.ARTICLE=C.ARTICLE
AND F.SALEOP in (1,3)
AND F.RECTYPE=1
and F.SALEDATE>=(select TO_DATE(Add_Months(last_day(sysdate),-2)+1,'DD.MM.YY') from dual)
AND F.SALEDATE<=(select TO_DATE(Add_Months(last_day(sysdate),-1),'DD.MM.YY') from dual)
AND (F.SALELOCATIONFROM = SL.ID OR F.SALELOCATIONTO = SL.ID)
),0) SALEQ,
nvl((SELECT
SUM((CASE WHEN F.SALEOP=3 THEN (-1)*F.SALESUM ELSE F.SALESUM END)) AS SALESUM
FROM SUPERMAG.FFMAPREP F
WHERE
F.ARTICLE=C.ARTICLE
AND F.SALEOP in (1,3)
AND F.RECTYPE=1
and F.SALEDATE>=(select TO_DATE(Add_Months(last_day(sysdate),-2)+1,'DD.MM.YY') from dual)
AND F.SALEDATE<=(select TO_DATE(Add_Months(last_day(sysdate),-1),'DD.MM.YY') from dual)
AND (F.SALELOCATIONFROM = SL.ID OR F.SALELOCATIONTO = SL.ID)
),0) SALESUM,
nvl((SELECT
SUM((CASE WHEN F.SALEOP=3 THEN (-1)*(F.SALESUM-F.PRIMECOST) ELSE (F.SALESUM-F.PRIMECOST) END)) AS DOXOD
FROM SUPERMAG.FFMAPREP F
WHERE
F.ARTICLE=C.ARTICLE
AND F.SALEOP in (1,3)
AND F.RECTYPE=1
and F.SALEDATE>=(select TO_DATE(Add_Months(last_day(sysdate),-2)+1,'DD.MM.YY') from dual)
AND F.SALEDATE<=(select TO_DATE(Add_Months(last_day(sysdate),-1),'DD.MM.YY') from dual)
AND (F.SALELOCATIONFROM = SL.ID OR F.SALELOCATIONTO = SL.ID)
),0) DOXOD,
(select price from SUPERMAG.smprices p where p.pricetype=1 and p.storeloc=13 and p.article=c.article) as price,
SL.ID as mx,
1 as period,
(case when (
select
count(*)
from
-- матрицы
SUPERMAG.SMASSORTMATRIXLOC ML, SUPERMAG.SMCARDMATRIX M,
--контракты
SUPERMAG.SMDOCUMENTS D1, SUPERMAG.SMCONTRACTLOCATIONS L1, SUPERMAG.SMSPEC S1, SUPERMAG.SMContracts SC1
where
-- матрицы
ML.IDMATRIX=M.IDMATRIX
AND ML.STORELOC=SL.ID
AND M.ARTICLE=C.ARTICLE
AND SL.ID = ML.STORELOC
AND M.ARTICLE=C.ARTICLE
--контракты
AND D1.ID = S1.DOCID
AND D1.DOCTYPE = S1.DOCTYPE
AND D1.DOCTYPE ='CO'
AND S1.ARTICLE = C.ARTICLE
AND SC1.ID=D1.ID
AND SC1.DOCTYPE=D1.DOCTYPE
AND SC1.BEGINSAT<=SYSDATE
AND ((SC1.ENDSAT>=SYSDATE) OR (SC1.ENDSAT IS Null))
AND D1.DOCSTATE=2
)>0 then 1 else 0 END) AS inMatrix
FROM
SUPERMAG.smcard c,
SUPERMAG.SMSTORELOCATIONS SL
WHERE
C.ACCEPTED =1
-- Только магазины
AND SL.ACCEPTED = 1
AND SL.IDCLASS= 1
AND SL.LOCTYPE = 4
-- /Только магазины