CREATE OR REPLACE VIEW CUSTOESTOQ
AS
SELECT POSESTDIA.CDFILIAL,
       POSESTDIA.CDALMOXARIFE,
       POSESTDIA.CDLOCALESTOQ,
       POSESTDIA.CDPRODUTO,
       POSESTDIA.NRLOTEESTQ,
       POSESTDIA.NRSUBLOTE,
       POSESTDIA.DTPOSIESTQ,
       POSESTDIA.QTESTOQDIA,
       POSESTDIA.VRESTOQDIA,
       POSESTDIA.VRESTOQDIAM1,
       POSESTDIA.VRESTOQDIAM2,
       POSESTDIA.VRESTOQBRUT,
       POSESTDIA.VRESTOQBRUM1,
       POSESTDIA.VRESTOQBRUM2,
       POSESTDIA.QTESTOQANT,
       POSESTDIA.DTCUSTANT,
       POSESTDIA.VRESTOQANT,
       POSESTDIA.VRESTOQANTM1,
       POSESTDIA.VRESTOQANTM2,
       POSESTDIA.VRBRUTOANT,
       POSESTDIA.VRBRUTOANTM1,
       POSESTDIA.VRBRUTOANTM2,
       POSESTDIA.IDPOSIZERO,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                           (POSESTDIA.VRESTOQANT/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQDIA/POSESTDIA.QTESTOQDIA)) AS VRCUST1,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                        (POSESTDIA.VRBRUTOANT/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQBRUT/POSESTDIA.QTESTOQDIA)) AS VRCUST2,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                           (POSESTDIA.VRESTOQANT/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQDIA/POSESTDIA.QTESTOQDIA)) AS VRCUST3,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                        (POSESTDIA.VRBRUTOANT/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQBRUT/POSESTDIA.QTESTOQDIA)) AS VRCUST4,
       PRODFILI.VRULTCOMPR   AS VRCUST5,
       PRODFILI.VRPRECULISTP AS VRCUST6,
       PRODFILI.VRPRECUAFOFI AS VRCUST7,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                           (POSESTDIA.VRESTOQANTM1/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQDIAM1/POSESTDIA.QTESTOQDIA)) AS VRCUST20,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                           (POSESTDIA.VRESTOQANTM2/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQDIAM2/POSESTDIA.QTESTOQDIA)) AS VRCUST21,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                        (POSESTDIA.VRBRUTOANTM1/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQBRUM1/POSESTDIA.QTESTOQDIA)) AS VRCUST22,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                        (POSESTDIA.VRBRUTOANTM2/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQBRUM2/POSESTDIA.QTESTOQDIA)) AS VRCUST23,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
              0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                        0, 0,
                       (POSESTDIA.VRESTOQANTM1/POSESTDIA.QTESTOQANT)),
                 (POSESTDIA.VRESTOQDIAM1/POSESTDIA.QTESTOQDIA)) AS VRCUST24,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
             0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                       0, 0,
                      (POSESTDIA.VRESTOQANTM2/POSESTDIA.QTESTOQANT)),
               (POSESTDIA.VRESTOQDIAM2/POSESTDIA.QTESTOQDIA)) AS VRCUST25,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
             0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                       0, 0,
                      (POSESTDIA.VRBRUTOANTM1/POSESTDIA.QTESTOQANT)),
               (POSESTDIA.VRESTOQBRUM1/POSESTDIA.QTESTOQDIA)) AS VRCUST26,
       DECODE(NVL(POSESTDIA.QTESTOQDIA, 0),
             0, DECODE(NVL(POSESTDIA.QTESTOQANT, 0),
                       0, 0,
                      (POSESTDIA.VRBRUTOANTM2/POSESTDIA.QTESTOQANT)),
               (POSESTDIA.VRESTOQBRUM2/POSESTDIA.QTESTOQDIA)) AS VRCUST27,
       PRODFILI.VRULTCOMPRM1 AS VRCUST28,
       PRODFILI.VRULTCOMPRM2 AS VRCUST29,
       PRODFILI.VRPRECULISM1 AS VRCUST30,
       PRODFILI.VRPRECULISM2 AS VRCUST31,
       PRODFILI.VRPRECUAFOM1 AS VRCUST32,
       PRODFILI.VRPRECUAFOM2 AS VRCUST33
FROM POSESTDIA, PRODFILI
WHERE (POSESTDIA.CDFILIAL    = PRODFILI.CDFILIAL)  AND
      (POSESTDIA.CDPRODUTO   = PRODFILI.CDPRODUTO)
/

