select * into #Temp6 from ConTrxTransactionMapping where ConsolidatedTransactionKey in (
SELECT SourceKey FROM RepIntegrationLog where flag='0' and status='1' and ErrorMessage like '%quantity%negative%' and sourcetype='200')
select a.ProductKey,Quantity, b.id,BookingWarehouseKey,c.Id as wid into #Temp7 from TrxTransactionSaleItem a ,
InvProduct b,InvWarehouse c where a.productkey=b.productkey
and a.BookingWarehouseKey=c.warehousekey and TransactionKey in (
select TransactionKey from #Temp6)
select SUM(quantity) as qty, id,wid into #Temp8 from #Temp7
group by Id,wid
select qty,OnHand,itemcode,WhsCode,qty-OnHand as gr from #Temp8 a,OITW b
where b.ItemCode=A.id collate SQL_Latin1_General_CP1_CI_AS
and b.WhsCode=a.wid collate SQL_Latin1_General_CP1_CI_AS
and qty <> OnHand and qty>OnHand
order by itemcode,WhsCode