Как-то так, хотя бред сивой кобылы получается...
Особенно при наличии неустановленной себестоимости.
Код:
select l.name LOC_NAME,
c.article ARTICLE,
w.incomeclientindex,
replace(c.name, chr(9), ' ') CARD_NAME,
g.quantity OSTATOK_TEK,
ROUND(w.ssr, 2) SSR,
trunc(SYSDATE - 1 +
FLOOR(decode(SIGN(g.quantity), -1, 0, g.quantity) / w.ssr)) OSTATOK_DO,
trunc(SYSDATE + 5) ZAKAZ_NA,
decode(SIGN(CEIL(w.ssr *
(5 -
FLOOR(decode(SIGN(g.quantity), -1, 0, g.quantity) /
w.ssr)))),
1,
CEIL(w.ssr *
(5 - FLOOR(decode(SIGN(g.quantity), -1, 0, g.quantity) /
w.ssr))),
0) ZAKAZ
from (select f.article,
f.salelocationfrom,
f.incomeclientindex,
sum(f.saleq / (to_date('21.06.2009', 'DD.MM.YYYY') -
to_date('01.05.2009', 'DD.MM.YYYY'))) SSR
from supermag.ffmaprep f
where f.rectype = 1
and f.saletype = 'CS'
and f.saledate between to_date('01.05.2009', 'DD.MM.YYYY') and
to_date('21.06.2009', 'DD.MM.YYYY')
and f.salelocationfrom in
(select l.id
from supermag.smstorelocations l
where l.idclass > 0
--and l.id in (5)
)
and f.article in (select c.article
from supermag.smcard c, supermag.sacardclass d
where c.idclass = d.id
and c.accepted = 1
and (d.tree like '2.%'))
group by f.article, f.salelocationfrom, f.incomeclientindex) w,
supermag.smgoods g,
supermag.smstorelocations l,
supermag.smcard c
where w.article = c.article
and w.article = g.article
and w.salelocationfrom = g.storeloc
and w.salelocationfrom = l.id
and c.accepted = 1
and decode(SIGN(CEIL(w.ssr *
(5 -
FLOOR(decode(SIGN(g.quantity), -1, 0, g.quantity) /
w.ssr)))),
1,
CEIL(w.ssr *
(5 - FLOOR(decode(SIGN(g.quantity), -1, 0, g.quantity) /
w.ssr))),
0) > 0
Order by LOC_NAME, CARD_NAME