1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
CREATE OR REPLACE PROCEDURE SOC1."MOY_VENDUE_LISSEE" as
begin
declare
cursor c is
with
vente_28_jours as (
select msk.codsoc, msk.sigdep, msk.codpro, msk.datmvt, sum(msk.qtemvt) somme
from msk
inner join pro on pro.codsoc = msk.codsoc and pro.codpro = msk.codpro and pro.codblocage in('VC','VN')
where msk.codsoc = 1 and msk.codosk = 'VMAG'
and msk.datmvt between to_char((sysdate - 28),'YYYYMMDD') and to_char(sysdate,'YYYYMMDD')
group by msk.codsoc, msk.sigdep, msk.codpro, msk.codosk, msk.datmvt
),
pic as (
select v.codsoc, v.sigdep, v.codpro, max(v.somme) valmax, avg(v.somme) valmoy
from vente_28_jours v
group by v.codsoc, v.sigdep, v.codpro
),
datmax as (
select v.codsoc, v.sigdep, v.codpro, min(v.datmvt) datmin
from vente_28_jours v
inner join pic p on p.codsoc = v.codsoc and p.sigdep = v.sigdep and p.codpro = v.codpro and p.valmax = v.somme and p.valmax >= p.valmoy * 5 or p.valmoy <= 1
group by v.codsoc, v.sigdep, v.codpro
)
select v.codsoc, v.sigdep, v.codpro, sum(v.somme) / decode(d.datmin, null, 28, 27) somme
from vente_28_jours v
left outer join datmax d on d.codsoc = v.codsoc and d.sigdep = v.sigdep and d.codpro = v.codpro
where v.datmvt != d.datmin
group by v.codsoc, v.sigdep, v.codpro, d.datmin
order by v.codsoc, v.sigdep, v.codpro;
cc c%rowtype;
cpt number(10);
begin
update dsk
set c15=0
where codsoc=1;
commit;
cpt:=0;
open c;
loop
fetch c into cc;
exit when c%notfound;
cpt:=cpt+1;
--commit;
update dsk
set c15=cc.somme
where codsoc=cc.codsoc
and sigdep=cc.sigdep
and codpro=cc.codpro;
if cpt=1000 then
commit;
cpt:=0;
end if;
end loop;
close c;
commit;
end;
end; |
Partager