select os_n.kol,
os_k.kol,
pr.kol,
os_n.article,
-- os_k.article,
os_n.shortname,
gc.idclass,
gr.tree,
gr.name
from
(select nvl(sum(quant),0) kol,
article,
shortname
from(
select sum(decode(t.doctype,'WI',t.quantity,'WO',-t.quantity,'CR',t.quantity,'CS',-t.quantity,'PD',t.quantity,'PE',-t.quantity)) quant,
t.article article,
nvl(d.locationto, d.locationfrom) loc,
c.shortname,
l.name mag
from smspec t, smdocuments d, smcard c, smstorelocations l
where t.doctype = d.doctype
and t.docid = d.id
and d.createdat < '01.01.2007'
and d.docstate > 2
and d.doctype in ('WI', 'WO', 'CS', 'CR', 'PE', 'PD')
and nvl(d.locationto, d.locationfrom) in (select id from smstorelocations where idclass=2 and id=2)
and l.id=nvl(d.locationto, d.locationfrom)
and t.article in (select article from smcard where idclass in (728))
and c.article=t.article
group by l.name, t.article, c.shortname, nvl(d.locationto, d.locationfrom))
group by article, shortname) os_n,
(select nvl(sum(quant),0) kol,
article,
shortname
from(
select sum(decode(t.doctype,'WI',t.quantity,'WO',-t.quantity,'CR',t.quantity,'CS',-t.quantity,'PD',t.quantity,'PE',-t.quantity)) quant,
t.article article,
nvl(d.locationto, d.locationfrom) loc,
c.shortname,
l.name mag
from smspec t, smdocuments d, smcard c, smstorelocations l
where t.doctype = d.doctype
and t.docid = d.id
and d.createdat < '01.02.2007'
and d.docstate > 2
and d.doctype in ('WI', 'WO', 'CS', 'CR', 'PE', 'PD')
and nvl(d.locationto, d.locationfrom) in (select id from smstorelocations where idclass=2 and id=2)
and l.id=nvl(d.locationto, d.locationfrom)
and t.article in (select article from smcard where idclass in (728))
and c.article=t.article
group by l.name, t.article, c.shortname, nvl(d.locationto, d.locationfrom))
group by article, shortname) os_k,
(select nvl(sum(quant),0) kol,
article,
shortname
from(
select sum(decode(t.doctype,'CS', t.quantity, 'CR', -t.quantity)) quant,
t.article article,
nvl(d.locationto, d.locationfrom) loc,
c.shortname,
l.name mag
from smspec t, smdocuments d, smcard c, smstorelocations l
where t.doctype = d.doctype
and t.docid = d.id
and d.createdat > '01.01.2007'
and d.createdat< '01.02.2007'
and d.docstate > 2
and d.doctype in ('CS', 'CR')
and nvl(d.locationto, d.locationfrom) in (select id from smstorelocations where idclass=2 and id=2)
and l.id=nvl(d.locationto, d.locationfrom)
and t.article in (select article from smcard where idclass in (728))
and c.article=t.article
group by l.name, t.article, c.shortname, nvl(d.locationto, d.locationfrom))
group by article, shortname) pr,
smcard gc,
sacardclass gr
where os_n.article=os_k.article
and gc.article=os_n.article
and gr.id=gc.idclass
-- and os_n.article=pr.article
Предполагается что запрос должен брать остатки на начало, на конец пеиода и продажи по кассам за период. Все данные запрос выбирает правильно, но если за период продался только один товар, а остатки по нескольким товарам то в поле pr.kol для всех товаров проставляется продажа только того артикула, который продавался за период.
При включенном and os_n.article=pr.article выбирается только одна строчка.
Использовал за основу отчет Mirt.