You are on page 1of 1

Query oracle (stock negative en almacn)

select codcia,
codalm,
codmat,
desmat,
sum( ingresos - salidas )
from
(
select c.codcia ,
trim(to_char(c.periodo)) || trim(to_char(c.nromes,'00')) as permes,
c.codalm ,
c.codmat ,
g.desmat,
sum( case when c.tipmov = 'I' then c.candes else 0 end ) as ingresos,
sum( case when c.tipmov = 'S' then c.candes else 0 end ) as salidas
from al_alma a,
al_tmov b ,
al_matg g ,
al_dmov c
where a.codcia = c.codcia and
a.codalm = c.codalm and
b.codcia = c.codcia and
b.tipmov = c.tipmov and
b.codmov = c.codmov and
g.codcia = c.codcia and
g.codmat = c.codmat and
c.flgest <> 'A' and
c.codalm <> 'AF1'
group by c.codcia ,
trim(to_char(c.periodo)) || trim(to_char(c.nromes,'00')) ,
c.codalm ,
c.codmat ,
g.desmat
)t
group by codcia,
codalm,
codmat,
desmat
having sum( ingresos - salidas ) < 0
order by codcia,
codalm,
codmat,
desmat;