while loop

hello guys,I am trying to calculate qty fields they want the calculate only when statusReceipt enum is not Ordered

I wrote like this:

While(qr.next())

{

{
if(InvenTtrans.statusreceipt !=statusreceipt::ordered)
itr=qr.get(Tablenum(Inventtrans));
CostvalueReceipt+=itr.CostAmountAdjustment + itr.CostAmountPosted;
qtyReceipt+=itr.Qty;//
}

}

qbr.value("<0");
//qbr2.value(sysquery::valueNotEmptyString());

qr=new queryrun(q);
While(qr.next())
{
if(InvenTtrans.statusreceipt !=statusreceipt::ordered)

{
itr=qr.get(Tablenum(Inventtrans));
CostvalueIssue+=itr.CostAmountAdjustment + itr.CostAmountPosted;
qtyIssue+=itr.Qty;
}

}

}
CostValue =costvaluereceipt + costValueissue;
Qty = qtyreceipt+qtyissue;

Normally It should see the all lines isnt it?but when I debug ,iin first line is ordered so it sees ordered and then break the loop but after ordered I have 3 lines also and there status Purchased registered and Received It must to calcute three of them.Then I changed my if statement I made like

if(InvenTtrans.statusreceipt ==statusreceipt::registered || InvenTtrans.statusreceipt ==statusreceipt::Purchased || InvenTtrans.statusreceipt ==statusreceipt::regcieved) but It didnot work also.

How should I calculate only this tree statusrecipt?

You don’t have to iterate all the records, you can just apply range on status receipt by using \Classes\SysQuery\valueNot

range.value(SysQuery::valueNot(statusreceipt::ordered));

Where you are finding inventTrans?

This should have been above the if condition and if condition should have used the same buffer (see below)

if(itr.statusreceipt !=statusreceipt::ordered)

You can also try to aggregate them by using sum on required field instead of looping the records.

oh so I can use if(itr.statusreceipt !=statusreceipt::ordered) under the while or I can give a range range.value(SysQuery::valueNot(statusreceipt::ordered)); isn’t it?

You can give a range, before running the query (before While(qr.next()))

Iterating all the records is extremely inefficient; all you need is a single query fetching three values. Something like this:

select sum (CostAmountAdjustment), sum(CostAmountPosted), sum(Qty)
    from InventTrans
    where StatusReceipt != StatusReceipt::Ordered

I have already suggested it. It seems he is not interested in doing it :slight_smile:

oh no sorry I could not try your solutions thats why I could not responed you guys ,I am gonna try your way thanks
both of you again for your helps

thank you martin I ll will try