SELECT c.article as "Артикул", c.name as "Наименование", NVL(q.quantity,0)-NVL(pr.quantity,0)+NVL(rs.quantity,0) as "Остаток", NVL((NVL(q.quantity,0)-NVL(pr.quantity,0)+NVL(rs.quantity,0))*NVL(p.price,0),0) as "$$$" FROM supermag.smcard c, supermag.smgoods q, supermag.sacardclass cc, supermag.smpricehistory p, ( SELECT dc.article, SUM(dc.quantity) as quantity, SUM(dc.itemprice) as price FROM supermag.smdocuments d, supermag.smspec dc WHERE d.doctype=dc.doctype AND d.id=dc.docid AND d.docstate=3 AND TO_DATE(d.createdat) >= ('01.07.2007') AND d.locationto = (2) GROUP BY dc.article ) pr, ( SELECT dc.article, SUM(dc.quantity) as quantity, SUM(dc.itemprice) as price FROM supermag.smdocuments d, supermag.smspec dc WHERE d.doctype=dc.doctype AND d.id=dc.docid AND d.docstate=3 AND TO_DATE(d.createdat) >= ('01.07.2007') AND d.locationfrom = (2) GROUP BY dc.article ) rs WHERE c.article=pr.article(+) AND c.article=rs.article(+) AND c.article=q.article(+) --AND c.accepted=1--active cards AND q.storeloc(+)=(2) AND c.idclass=cc.id(+) AND p.storeloc(+)=(2) AND p.pricetype(+)=0 AND p.eventtime=(select max(eventtime) from supermag.smpricehistory where eventtime <= to_date('01.07.2007','DD.MM.YYYY') and storeloc = q.storeloc and article = q.article) AND p.article(+)=c.article AND cc.tree like '1.1.1.%' AND (NVL(q.quantity,0)-NVL(pr.quantity,0)+NVL(rs.quantity,0))>=0-- > 0 condition
SELECT c.article as "Артикул", c.name as "Наименование", NVL(q.quantity,0)-NVL(pr.quantity,0)+NVL(rs.quantity,0) as "Остаток", NVL((NVL(q.quantity,0)-NVL(pr.quantity,0)+NVL(rs.quantity,0))* NVL((SELECT his.PRICE FROM SUPERMAG.SMPRICEHISTORY his, SUPERMAG.SMLOCPRICES prt WHERE his.STORELOC = q.STORELOC AND his.ARTICLE = q.ARTICLE AND his.PRICETYPE = prt.PRICETYPE AND prt.LOCID = his.STORELOC AND (bitand(prt.FLAGS, 2) = 2) AND his.RECID = (SELECT MAX(h.RECID) FROM SUPERMAG.SMPRICEHISTORY h WHERE h.STORELOC = his.STORELOC AND h.ARTICLE = his.ARTICLE AND h.PRICETYPE = his.PRICETYPE AND TRUNC(h.EVENTTIME) <= TRUNC(TO_DATE('01.07.2007', 'DD.MM.YYYY')))),0),0) as "$$$" FROM supermag.smcard c, supermag.smgoods q, supermag.sacardclass cc, ( SELECT dc.article, SUM(dc.quantity) as quantity, SUM(dc.itemprice) as price FROM supermag.smdocuments d, supermag.smspec dc WHERE d.doctype=dc.doctype AND d.id=dc.docid AND d.docstate=3 AND TO_DATE(d.createdat) >= to_date('01.07.2007', 'DD.MM.YYYY') AND d.locationto = (2) GROUP BY dc.article ) pr, ( SELECT dc.article, SUM(dc.quantity) as quantity, SUM(dc.itemprice) as price FROM supermag.smdocuments d, supermag.smspec dc WHERE d.doctype=dc.doctype AND d.id=dc.docid AND d.docstate=3 AND TO_DATE(d.createdat) >= to_date('01.07.2007', 'DD.MM.YYYY') AND d.locationfrom = (2) GROUP BY dc.article ) rs WHERE c.article=pr.article(+) AND c.article=rs.article(+) AND c.article=q.article(+) --AND c.accepted=1--active cards AND q.storeloc(+)=(2) AND c.idclass=cc.id(+) AND cc.tree like '1.1.1.%' AND (NVL(q.quantity,0)-NVL(pr.quantity,0)+NVL(rs.quantity,0))>=0-- > 0 condition
NVL((SELECT his.PRICE FROM SUPERMAG.SMPRICEHISTORY his, SUPERMAG.SMLOCPRICES prt WHERE his.STORELOC = q.STORELOC AND his.ARTICLE = q.ARTICLE AND his.PRICETYPE = prt.PRICETYPE AND prt.LOCID = his.STORELOC AND (bitand(prt.FLAGS, 2) = 2) AND his.RECID = (SELECT MAX(h.RECID) FROM SUPERMAG.SMPRICEHISTORY h WHERE h.STORELOC = his.STORELOC AND h.ARTICLE = his.ARTICLE AND h.PRICETYPE = his.PRICETYPE AND TRUNC(h.EVENTTIME) <= TRUNC(TO_DATE('01.07.2007', 'DD.MM.YYYY')))),0),0) as "$$$"