You are on page 1of 2

drop table promemo

drop table conanmemo


drop table anoanmemo

CREATE TABLE promemo(cliente int,sumi int,promedio int)


INSERT INTO promemo
select a.clicod,a.sumnro,round((sum(cnskwcns)/COUNT(*)),0) as promedio from consum
b right outer join sumser a on(a.clicod=b.CliCod and a.SumNro=b.SumNro and
(cnsano*100+cnsmes) between '201608' and '201702')
where a.SrvCod=1 and cnssuc=1 group by a.CliCod,a.SumNro order by 1,2
update promemo set promedio=0 where promedio is NULl
CREATE TABLE conanmemo(cliente int,sumi int,consu_ant int)
INSERT INTO conanmemo
select a.clicod,a.sumnro,cnskwcns from consum b right outer join sumser a
on(a.clicod=b.CliCod and a.SumNro=b.SumNro and (cnsano*100+cnsmes)='201612') where
a.SrvCod=1 and cnssuc=1 order by 1,2
update conanmemo set consu_ant=0 where consu_ant is NULL
CREATE TABLE anoanmemo(cliente int,sumi int,ano_ant int)
INSERT INTO anoanmemo
select a.clicod,b.sumnro,cnskwcns from consum b right outer join sumser a
on(a.clicod=b.CliCod and a.SumNro=b.SumNro and (CnsAno*100+CnsMes)='201612') where
a.SrvCod=1 and cnssuc=1 order by 1,2
update anoanmemo set ano_ant=0 where ano_ant is NULL

select SumRut,SumOrd,clinom,SUMUBI as
domicilio,a.CliCod,b.sumnro,d.sum6mdd,d.Sum6LecU,case when e.consu_ant is NULL THEN
0 ELSE e.consu_ant end as consu_ant,case when f.promedio is NULL then 0 else
f.promedio end as promedio,case when g.ano_ant is null then case when promedio is
NULL then case when consu_ant is NULL then 0 end else promedio end else ano_ant end
ano_ant,'0.50' as tolerancia,i.MddEnt,d.Sum6Mlt,Me1NroInt,1 as servicio, 0 as
LatitudReg, 0 as LongitudReg, 'Remesa' as AdicioanlEnt1, SUMREM as AdcionalEnt2,
'prueba 3' as AdicionalENt3
from socios a,sumser c,SUMMED d, MEDNUM h, MEDIDO i, SUMINI b
left outer join conanmemo e on (clicod=e.cliente and sumnro=e.sumi) left outer join
promemo f on (CliCod=f.cliente and SumNro=f.sumi)
left outer join anoanmemo g on (CliCod=g.cliente and SumNro=g.sumi)
where a.CliCod=b.CliCod and b.CliCod=d.CliCod and b.SumNro=d.SumNro and
b.clicod=c.CliCod and b.SumNro=c.SumNro and h.MddCod=i.MddCod
and c.SrvCod=1 and h.SrvCod=c.SrvCod and i.Mddcod=d.Sum6tpomdd AND SUMREM IN(242)
and c.srvcod=d.srvcod and c.Sum2Sts in('1','2','3') and d.Sum6FecBaj='01-01-0001'
and h.me1sts=1 and d.sum6mdd=h.Me1Nro and i.SrvCod=1-- and sumrem between '1' and
'4'
--and sumrut in(1,16,24)and
and sumare=1600 and sumrem=242
--and sumrut in(53,....) para seleccionar rutas
order by 1,2

create table MEMOTEMPORAL (ruta int, cliente int,sumionistro int, medidor


varchar(10), lectura int, ruedas int)
insert into memotemporal
select SumRut,a.CliCod,b.sumnro,d.sum6mdd,d.Sum6LecU,i.MddEnt
from socios a,sumser c,SUMMED d, MEDNUM h, MEDIDO i, SUMINI b
left outer join conanmemo e on (clicod=e.cliente and sumnro=e.sumi) left outer join
promemo f on (CliCod=f.cliente and SumNro=f.sumi)
left outer join anoanmemo g on (CliCod=g.cliente and SumNro=g.sumi)
where a.CliCod=b.CliCod and b.CliCod=d.CliCod and b.SumNro=d.SumNro and
b.clicod=c.CliCod and b.SumNro=c.SumNro and h.MddCod=i.MddCod
and c.SrvCod=1 and h.SrvCod=c.SrvCod and i.Mddcod=d.Sum6tpomdd AND SUMREM IN(242)
and c.srvcod=d.srvcod and c.Sum2Sts in('1','2','3') and d.Sum6FecBaj='01-01-0001'
and h.me1sts=1 and d.sum6mdd=h.Me1Nro and i.SrvCod=1-- and sumrem between '1' and
'4'
--and sumrut in(1,16,24)and
and sumare=1600 and sumrem=242
--and sumrut in(53,....) para seleccionar rutas
order by 1,2

select * from memotemporal


update memotemporal set ruedas=

You might also like