# How to calculate sum "Remaining Quantity" field

Navision 2.60 RU Is it possible to check with queries if the fields are correct in table 27(Item) .That to field are flowfields from Item Ledger Entry (Quantity and Remaining Quantity). We have difference in this 2 fields in Item table, thought know that these fields should be equal. There were error in canceling to invoices so the item that were in that invoices now has differences. This is what i did --1) are all entries that exist in Item Application (339) exist in Item Ledger Entry (339) declare @no int, @ileno int, @iato int, @iafrom int, @qty decimal(38, 20), @post_date datetime, @message varchar(140) declare ia cursor for select [Entry No.], [Item Ledger Entry No.], [Inbound Item Entry No.], [Outbound Item Entry No.], [Qty], [Posting Date] from [FIRMA\$Item Application Entry] where [Outbound Item Entry No.] <> 0 and [Qty] < 0 and [osting Date] >= ‘20041001’ open ia fetch next from ia into @no, @ileno, @iato, @iafrom, @qty, @post_date while @@fetch_status = 0 begin if exists (select 1 from [FIRMA\$Item Ledger Entry] where [Entry No.] = @ileno) and exists (select 1 from [FIRMA\$Item Ledger Entry] where [Entry No.] = @iato) and exists (select 1 from [FIRMA\$Item Ledger Entry] where [Entry No.] = @iafrom) begin if @iato = @iafrom begin select @message = cast(@no as char(10)) + ’ ’ + cast(@ileno as char(10)) + ’ ’ + cast(@iato as char(10)) + ’ ’ + cast(@iafrom as char(10)) + ’ ’ + /*cast(round(@qty, 1) as varchar(20) ) + */’ ’ + convert(char(10), @post_date,4) print @message end end else begin select @message = cast(@no as char(10)) + ’ ’ + cast(@ileno as char(10)) + ’ ’ + cast(@iato as char(10)) + ’ ’ + cast(@iafrom as char(10)) + ’ ’ + /cast(round(@qty, 1) as varchar(20) ) + /’ ’ + convert(char(10), @post_date,4) print @message end fetch next from ia into @no, @ileno, @iato, @iafrom, @qty, @post_date end close ia deallocate ia – 2) are fields “Remaining Quantity” in table Item Ledger Entry 32 equals to sum of rows (Quantity) in Item Application Table (339) select a.,a.[Qty] - b.iapQty diff,b. from ( select [Entry No.], [Document No.], [Posting Date], [Item No.],[Qty], [Remaining Qty] from [FIRMA\$Item Ledger Entry] where Positive = 0 and [Posting Date] >= ‘20050101’ and [Qty] <> 0 and [Item No.] = ‘Ï-Ì-000501’) a, ( select [Outbound Item Entry No.],sum([Qty]) iapQty from [FIRMA\$Item Application Entry] group by [Outbound Item Entry No.], having sum([Qty]) <> 0) b where a.[Entry No.] = b.[Outbound Item Entry No]