select storeloc,article, count(*) from( select storeloc,article,sum(qu) over (partition by storeloc,article order by dt desc range unbounded preceding) ost, dt from( select storeloc,article,sum(quantity) qu, dt from( select t.storeloc, t.article, t.quantity, to_date(sysdate) dt from smgoods t union all select d.locationto, a.article, -a.quantity, d.createdat dt from smspec a, smdocuments d where a.doctype=d.doctype and a.docid=d.id and d.createdat between :DateStart and sysdate and d.docstate>=2 and d.opcode in (0,3,9,13,18) union all select d.locationfrom, a.article, a.quantity, d.createdat dt from smspec a, smdocuments d where a.doctype=d.doctype and a.docid=d.id and d.createdat between &DateStart and sysdate and d.docstate>=2 and d.opcode in (1,2,7,8,12) union all select c.id storeLoc, b.article Article,0 quantity,a.dt dt from dates a,smcard b,smstorelocations c where a.dt between :DateStart and sysdate and c.id<>-1) group by storeloc,article, dt) ) where ost<=0 group by storeloc, article
create table DATES ( DT DATE ) tablespace USERS pctfree 10 pctused 40 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); insert into dates(dt) select to_date('01.01.2006')+rownum from all_objects where rownum < 366);
and d.createdat between &DateStart and sysdate
opcode in (1,2,3,4,5,6)
opcode=1 or opcode=2 or opcode=3 or opcode=4 or opcode=5 or opcode=6
and d.createdat between &DateStart and sysdate
select storeloc,article, count(*) from( select storeloc,q.article,sum(qu) over (partition by storeloc,q.article order by dt desc range unbounded preceding) ost, dt from( select storeloc,article,sum(quantity) qu, dt from( select t.storeloc, t.article, t.quantity, to_date(sysdate) dt from smgoods t, smcard m, sacardclass y where t.article=m.article and y.id=m.idclass and y.tree like CONCAT(&Group,'%') union all select d.locationto, a.article, -a.quantity, d.createdat dt from smspec a, smdocuments d, smcard m, sacardclass y where a.doctype=d.doctype and a.docid=d.id and d.createdat between to_date(&DateStart) and to_date(sysdate) and d.docstate>=2 and d.opcode in (0,3,9,13,18) and a.article=m.article and y.id=m.idclass and y.tree like CONCAT(&Group,'%') union all select d.locationfrom, a.article, a.quantity, d.createdat dt from smspec a, smdocuments d, smcard m, sacardclass y where a.doctype=d.doctype and a.docid=d.id and d.createdat between to_date(&DateStart) and to_date(sysdate) and d.docstate>=2 and d.opcode in (1,2,7,8,12) and a.article=m.article and y.id=m.idclass and y.tree like CONCAT(&Group,'%') union all select c.id storeLoc, b.article Article,0 quantity,a.dt dt from dates a,smcard b,smstorelocations c, sacardclass y where a.dt between to_date(&DateStart) and to_date(sysdate) and b.idclass=y.id and y.tree like CONCAT(&Group,'%') and c.id<>-1) group by storeloc,article, dt) q ) where ost>0 and dt between to_date(&dateStart) and to_date(&DateEnd) group by storeloc, article
select t.storeloc, t.article, t.quantity, to_date(sysdate) dt from smgoods t, smcard m, sacardclass y where t.article=m.article and y.id=m.idclass and y.tree like CONCAT(&Group,'%') union all select d.locationto, a.article, -a.quantity, d.createdat dt from smspec a, smdocuments d, smcard m, sacardclass y where a.doctype=d.doctype and a.docid=d.id and d.createdat between to_date(&DateStart) and to_date(sysdate) and d.docstate>=2 and d.opcode in (0,3,9,13,18) and a.article=m.article and y.id=m.idclass and y.tree like CONCAT(&Group,'%') union all select d.locationfrom, a.article, a.quantity, d.createdat dt from smspec a, smdocuments d, smcard m, sacardclass y where a.doctype=d.doctype and a.docid=d.id and d.createdat between to_date(&DateStart) and to_date(sysdate) and d.docstate>=2 and d.opcode in (1,2,7,8,12) and a.article=m.article and y.id=m.idclass and y.tree like CONCAT(&Group,'%')
over (partition by storeloc,q.article order by dt desc range unbounded preceding)
create or replace package pivot as type date_range_tbl is table of date; function gen_range(start_date date, end_date date) return date_range_tbl pipelined; end; / create or replace package body pivot as function gen_range(start_date date, end_date date) return date_range_tbl pipelined is cur_date date; begin cur_date := trunc(start_date); while (cur_date <= trunc(end_date)) loop pipe row(cur_date); cur_date := cur_date + 1; end loop; return; end; end; / select t.column_value from table(pivot.gen_range('01.06.2006', '31.08.2006')) t