После того как все проверили - формируем 2 таблицы - приход и оборот.
1. Приход:
Код:
INSERT INTO supermag.exporter_alcoprihod
SELECT '2012' god, '1' kvart, idmx, idpost, clientdoc, clientdate,
SUM (rate * quantity) / 10 dkl, s1, idprod, idlic
FROM (SELECT d.idmx, b.ID idpost, c.supplierdoc clientdoc,
c.supplinvoicecreate clientdate, e.quantity,
NVL ((SELECT rate
FROM supermag.smaltunits
WHERE e.article = article AND altunit = '2'),
0
) rate,
SUBSTR (NVL ((SELECT propval
FROM supermag.smcardproperties
WHERE e.article = article
AND propid = 'alcokod1'),
'-'
),
0,
3
) s1,
NVL ((SELECT propval
FROM supermag.smcardproperties
WHERE e.article = article
AND propid = 'alcoproducer'),
'5000'
) idprod,
NVL
((SELECT idsuplic
FROM supermag.exporter_alcolic
WHERE idsuplic > 0
AND idsup = a.clientindex
AND c.supplinvoicecreate BETWEEN datastart AND dataend),
0
) idlic
FROM supermag.smdocuments a,
supermag.exporter_alcolic b,
supermag.smwaybillsin c,
supermag.exporter_alcomx d,
supermag.smspec e,
supermag.smcard g
WHERE a.doctype = 'WI'
AND a.createdat BETWEEN TO_DATE ('01.01.2012', 'DD.MM.YYYY')
- 10
AND TO_DATE ('31.03.2012', 'DD.MM.YYYY')
+ 10
AND c.doctype = 'WI'
AND a.ID = c.ID
AND a.locationto = d.idmx
AND b.idsuplic = 0
AND a.clientindex = b.idsup
AND c.supplinvoicecreate BETWEEN TO_DATE ('01.01.2012',
'DD.MM.YYYY'
)
AND TO_DATE ('31.03.2012',
'DD.MM.YYYY'
)
AND e.doctype = 'WI'
AND e.docid = a.ID
AND e.article = g.article
AND g.idclass IN (SELECT f.ID
FROM supermag.sacardclass f
WHERE f.tree LIKE ('1.1.%')))
GROUP BY idmx, idpost, clientdoc, clientdate, s1, idprod, idlic
ORDER BY s1, idprod
2. Оборот:
Код:
INSERT INTO supermag.exporter_alcooborot
SELECT '2012' god, '1' kvart, mxid, p03, idprod,
SUM (p06 * rate) / 10 p06i, SUM (p08 * rate) / 10 p08i,
SUM (p11 * rate) / 10 p11i, SUM (p12 * rate) / 10 p12i,
SUM (p15 * rate) / 10 p15i, SUM (p16 * rate) / 10 p16i,
SUM (p17 * rate) / 10 p17i, SUM (p20 * rate) / 10 p20i
FROM (SELECT z.mxid,
SUBSTR (NVL ((SELECT propval
FROM supermag.smcardproperties
WHERE z.art = article
AND propid = 'alcokod1'),
'000'
),
0,
3
) p03,
NVL ((SELECT propval
FROM supermag.smcardproperties
WHERE z.art = article AND propid = 'alcoproducer'),
'5000'
) idprod,
z.art,
NVL ((SELECT rate
FROM supermag.smaltunits
WHERE z.art = article AND altunit = 2), 1) rate,
z.p06, z.p08, z.p11, z.p12, z.p15, z.p16, z.p17, z.p20
FROM (SELECT mxid, art, SUM (s6) p06, SUM (s8) p08,
SUM (s11) p11, SUM (s12) p12, SUM (s15) p15,
SUM (s16) p16, SUM (s17) p17, SUM (s20) p20
FROM (SELECT mxid, art, s6, 0 s8, 0 s11, 0 s12,
0 s15, 0 s16, 0 s17, 0 s20
FROM (SELECT NVL (d.locationto,
d.locationfrom
) mxid,
t.article art,
SUM (DECODE (t.doctype,
'WI', t.quantity,
'WO', -t.quantity,
'CR', t.quantity,
'CS', -t.quantity
)
) s6
FROM supermag.smspec t,
supermag.smdocuments d
WHERE t.doctype = d.doctype
AND t.docid = d.ID
AND d.createdat <
TO_DATE ('01.01.2012',
'DD.MM.YYYY'
)
AND d.docstate = 3
AND d.doctype IN
('WI', 'WO', 'CS', 'CR')
AND t.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
GROUP BY NVL (d.locationto,
d.locationfrom
),
t.article)
WHERE s6 != 0
UNION ALL
SELECT a.locationto mxid, b.article art, 0 s6,
b.quantity s8, 0 s11, 0 s12, 0 s15,
0 s16, 0 s17, 0 s20
FROM supermag.smdocuments a,
supermag.smspec b
WHERE a.doctype = 'WI'
AND a.createdat BETWEEN TO_DATE
('01.01.2012',
'dd.mm.yyyy'
)
AND TO_DATE
('31.03.2012',
'dd.mm.yyyy'
)
AND b.doctype = 'WI'
AND a.docstate = 3
AND a.opcode = 0
AND a.userop IS NULL
AND b.docid = a.ID
AND b.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
UNION ALL
SELECT a.locationto mxid, b.article art, 0 s6,
0 s8, b.quantity s11, 0 s12, 0 s15,
0 s16, 0 s17, 0 s20
FROM supermag.smdocuments a,
supermag.smspec b
WHERE a.doctype = 'CR'
AND a.createdat BETWEEN TO_DATE
('01.01.2012',
'dd.mm.yyyy'
)
AND TO_DATE
('31.03.2012',
'dd.mm.yyyy'
)
AND b.doctype = 'CR'
AND b.docid = a.ID
AND b.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
UNION ALL
SELECT a.locationto mxid, b.article art, 0 s6,
0 s8, 0 s11, b.quantity s12, 0 s15,
0 s16, 0 s17, 0 s20
FROM supermag.smdocuments a,
supermag.smspec b
WHERE a.doctype = 'WI'
AND a.createdat BETWEEN TO_DATE
('01.01.2012',
'dd.mm.yyyy'
)
AND TO_DATE
('31.03.2012',
'dd.mm.yyyy'
)
AND b.doctype = 'WI'
AND a.docstate = 3
AND a.opcode = 9
AND b.docid = a.ID
AND b.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
UNION ALL
SELECT a.locationfrom mxid, b.article art,
0 s6, 0 s8, 0 s11, 0 s12,
b.quantity s15, 0 s16, 0 s17, 0 s20
FROM supermag.smdocuments a,
supermag.smspec b
WHERE a.doctype = 'CS'
AND a.createdat BETWEEN TO_DATE
('01.01.2012',
'dd.mm.yyyy'
)
AND TO_DATE
('31.03.2012',
'dd.mm.yyyy'
)
AND b.doctype = 'CS'
AND b.docid = a.ID
AND b.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
UNION ALL
SELECT a.locationfrom mxid, b.article art,
0 s6, 0 s8, 0 s11, 0 s12, 0 s15,
b.quantity s16, 0 s17, 0 s20
FROM supermag.smdocuments a,
supermag.smspec b
WHERE a.doctype = 'WO'
AND a.docstate = 3
AND (a.opcode = 8 OR a.opcode = 7)
AND a.createdat BETWEEN TO_DATE
('01.01.2012',
'dd.mm.yyyy'
)
AND TO_DATE
('31.03.2012',
'dd.mm.yyyy'
)
AND b.doctype = 'WO'
AND b.docid = a.ID
AND b.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
UNION ALL
SELECT a.locationfrom mxid, b.article art,
0 s6, 0 s8, 0 s11, 0 s12, 0 s15, 0 s16,
b.quantity s17, 0 s20
FROM supermag.smdocuments a,
supermag.smspec b
WHERE a.doctype = 'WO'
AND a.docstate = 3
AND a.opcode = 2
AND a.createdat BETWEEN TO_DATE
('01.01.2012',
'dd.mm.yyyy'
)
AND TO_DATE
('31.03.2012',
'dd.mm.yyyy'
)
AND b.doctype = 'WO'
AND b.docid = a.ID
AND b.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
UNION ALL
SELECT mxid, art, 0 s6, 0 s8, 0 s11, 0 s12,
0 s15, 0 s16, 0 s17, s20
FROM (SELECT NVL (d.locationto,
d.locationfrom
) mxid,
t.article art,
SUM (DECODE (t.doctype,
'WI', t.quantity,
'WO', -t.quantity,
'CR', t.quantity,
'CS', -t.quantity
)
) s20
FROM supermag.smspec t,
supermag.smdocuments d
WHERE t.doctype = d.doctype
AND t.docid = d.ID
AND d.createdat <
TO_DATE ('31.03.2012',
'DD.MM.YYYY'
)
+ 1
AND d.docstate = 3
AND d.doctype IN
('WI', 'WO', 'CS', 'CR')
AND t.article IN (
SELECT c.article
FROM supermag.smcard c
WHERE c.idclass IN (
SELECT ID
FROM supermag.sacardclass
WHERE tree LIKE
('1.1.%'
)))
GROUP BY NVL (d.locationto,
d.locationfrom
),
t.article)
WHERE s20 != 0)
GROUP BY mxid, art) z)
GROUP BY mxid, p03, idprod
ORDER BY mxid, p03, idprod