select c.sCodigo      sCodItem,
                   c.sDescricao   sItem,
                   e.sUnidade     sUni,

                   e1.sUFis      sUn1,
                   f1.sUFis      sUn2,
                   g1.sUFis      sUn3,
                   h1.sUFis      sUn4,
                   i1.sUFis      sUn5,
                   j1.sUFis      sUn6,
                   k1.sUFis      sUn7,
                   l1.sDescricao sIns,
                   c1.sCodigo    sUnidade,

                   f.sSigla,

               sum(a.nQt),
               sum(a.nQtRes),
               sum(a.nQt-a.nQtRes),
                  (b1.nSdo),
                  (b1.nRes),
                  (b1.nSdo-b1.nRes),
                  ((b1.nSdo-b1.nRes)-sum(a.nQt-a.nQtRes)),
               sum(((a.nQt-a.nQtRes)*c.nPB))
              from Ordens_Itens   a      join Ordens              b on a.pOrdem         = b.pPId     and  b.iVer = b.iVAt and b.lExc = 0 and b.tConf is not null and b.jTipo = 1 and b.lGlobal = 1 and b.lGerarPreNF = 0
                                         join Itens               c on a.pItem          = c.pPId     and  c.iVer = c.iVAt and c.jTipo= 0 
                                         join NA_Painel           d on b.pNA_EGlobal    = d.pPId     and  d.iVer = d.iVat and d.lExc = 0 and d.lOnOff = 1
                                         join CUP                 f on b.pEmi           = f.pPId     and  f.iVer = f.iVAt
                                    left join UnidadeMedida       e on a.pUni           = e.pPId     and  e.iVer = e.iVAt

                                    left join EST_RegDepositos   b1 on a.pRegDepVen     = b1.pPId    and b1.iVer = b1.iVAt
                                    left join EST_Depositos      bb on b1.pDep          = bb.pPId    and bb.iVer = bb.iVAt
                                    left join UFis               e1 on bb.pUFis         = e1.pPId    and e1.iVer = e1.iVAt
                                    left join UFis               f1 on e1.pParent       = f1.pPId    and f1.iVer = f1.iVAt
                                    left join UFis               g1 on f1.pParent       = g1.pPId    and g1.iVer = g1.iVAt
                                    left join UFis               h1 on g1.pParent       = h1.pPId    and h1.iVer = h1.iVAt
                                    left join UFis               i1 on h1.pParent       = i1.pPId    and i1.iVer = i1.iVAt
                                    left join UFis               j1 on i1.pParent       = j1.pPId    and j1.iVer = j1.iVAt
                                    left join UFis               k1 on j1.pParent       = k1.pPId    and k1.iVer = k1.iVAt
                                    left join EST_Instancias     l1 on b1.pIns          = l1.pPId    and l1.iVer = l1.iVAt
                                    left join UnidadeMedida      c1 on b1.pUni          = c1.pPId    and c1.iVer = c1.iVAt
              where a.lExc   = 0
                 and cast(b.tEmissao as date) >= '01-Jun-2017' and cast(b.tEmissao as date) <= '16-Jun-2017'

                and a.iVer   = a.iVAt
--                and (a.nQt-a.nQtRes) > (b1.nSdo-b1.nRes)
            group by c.sCodigo,
                     c.sDescricao,
                     e.sUnidade,
                     bb.pUFis,
                     e1.sUFis,
                     f1.sUFis,
                     g1.sUFis,
                     h1.sUFis,
                     i1.sUFis,
                     j1.sUFis,
                     k1.sUFis,
                     l1.sDescricao,
                     c1.sCodigo,
                     f.sSigla,
                     b1.nSdo,
                     b1.nRes
            having ((b1.nSdo-b1.nRes)-sum(a.nQt-a.nQtRes))<0
 order by  6 DESC,1