Select Item_No,Store_Id,Sum(COALESCE(REQUIRED,0)),Sum(COALESCE(AVAILABLE,0)), Sum(COALESCE(AVAILABLE,0)-COALESCE(REQUIRED,0)) From (Select Item_no,Store_Id,Inv_Batches.BatchNo,REQUIRED,AVAILABLE,(AVAILABLE-REQUIRED) Dif From  (select M.ITEM_NO, M.STORE_ID, M.BatchId , SUM(COALESCE((M.QTY+m.gIFT),0) ) REQUIRED,
     ( SELECT SUM(COALESCE(QTY_IN,0) - COALESCE(QTY_OUT,0) )
       FROM ITEM_QTY IQ
       where IQ.ITEM_NO = M.ITEM_NO and Iq.Batchid=M.Batchid  And IQ.STORE_ID = M.STORE_ID) AVAILABLE
  From invoices I left join movements M on (m.INVOICE_BOND_SERIAL = I.INVOICE_SERIAL ) , ITEMS
  where ITEMS.ITEM_NO = M.ITEM_NO and ITEMS.CTYPE <> 2 and ITEMS.CTYPE <> 8   AND  I.INV_TYPE = '1' AND  STATUS = "U" And M.BatchId Is Not Null   and IYear >= 2020
   group by   M.ITEM_NO, M.STORE_ID ,M.batchid ) AVQty  Join Inv_Batches On  AVQty.BatchId=Inv_Batches.BatchId )
   Group By Item_no,Store_Id
