Skip to main content
Solved

Duplicate results and divide by zero errors

  • January 7, 2025
  • 9 replies
  • 113 views

Forum|alt.badge.img

Hello all,

I have two issues with a generic inquiry I’ve written. I’ve tried (and mostly succeeded) to make an expanded Sales Order GI that includes information on the associated production orders.

First issue: I created a margin % column in the result grid, and it calculates properly but when I attempt to sort the column ascending/descending, I get a divide by 0 error:

=Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2)

I also have the same issue with the markup result I created:

=Round(CDbl([SOLine.CuryLineAmt]/[AMProdItem.WIPTotal]),2)

I’m assuming it’s because there are a few orders with a 0 value and I can’t divide by 0… is there a way to include a IIF in my expression above to exclude any 0 values?

A bigger issue -- duplicate results. After making a few additional table joins/relations in the GI, I now have multiple duplicate sales orders in my results.

If a sales order has one sales order line, that SO should appear once (ex. line 1). If a sales order has two lines, it should appear twice (line 1, line 2), etc.

Thank you for any ideas… I’m terrible with joins and may’ve missed something… thanks!

 

Best answer by amitr70

Two workarounds

  1. Try to add grouping if you are getting duplicate records
  2. Extract the working query from Traces screen. And tweak it according in a view. Use that view in your GI. Move all calculations to your SQL view. This will keep your GI clean and simple.
View original
Did this topic help you find an answer to your question?

9 replies

Forum|alt.badge.img+1
  • Jr Varsity III
  • 81 replies
  • January 7, 2025

Hi ​@swartzfeger , To resolve divide by zero error, add if condition like below

=IIf([SOLine.CuryLineAmt]>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)

=IIf([AMProdItem.WIPTotal]>0, Round(CDbl([SOLine.CuryLineAmt]/[AMProdItem.WIPTotal]),2),0)

 

Please check the table joins and fields to resolve duplicate issue


Forum|alt.badge.img
  • Author
  • Freshman II
  • 180 replies
  • January 7, 2025
arthia98 wrote:

Hi ​@swartzfeger , To resolve divide by zero error, add if condition like below

=IIf([SOLine.CuryLineAmt]>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)

=IIf([AMProdItem.WIPTotal]>0, Round(CDbl([SOLine.CuryLineAmt]/[AMProdItem.WIPTotal]),2),0)

 

Please check the table joins and fields to resolve duplicate issue

Thanks Arthia, I added those conditions but it’s the same result. If I sort Ascending, it sorts fine; if I sort by Descending, I still get the divide by 0.

I found a superfluous relation that I deactivated but still figuring out the joins (I’m a sql idiot… left, right, inner, full, cross...)


amitr70
Semi-Pro I
Forum|alt.badge.img
  • Semi-Pro I
  • 30 replies
  • January 7, 2025

@swartzfeger 
Try this
=IIF(ISNULL([SOLine.CuryLineAmt],0)<>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)


Forum|alt.badge.img
  • Author
  • Freshman II
  • 180 replies
  • January 7, 2025
amitr70 wrote:

@swartzfeger 
Try this
=IIF(ISNULL([SOLine.CuryLineAmt],0)<>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)

Thanks Amit, but when I sort by descending it still returns a Divide By 0 error.


Forum|alt.badge.img
  • Author
  • Freshman II
  • 180 replies
  • January 7, 2025
arthia98 wrote:

Hi ​@swartzfeger , To resolve divide by zero error, add if condition like below

=IIf([SOLine.CuryLineAmt]>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)

=IIf([AMProdItem.WIPTotal]>0, Round(CDbl([SOLine.CuryLineAmt]/[AMProdItem.WIPTotal]),2),0)

 

Please check the table joins and fields to resolve duplicate issue

amitr70 wrote:

@swartzfeger 
Try this
=IIF(ISNULL([SOLine.CuryLineAmt],0)<>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)

Ok, I was wrong -- these both work -- thank you!

The issue appears to be the AMBomMatl table/relation --

When I deactivate the AMBomMatl table and fields, the IIF conditions work and sort properly, and the duplicates go away! But I need those two fields from the AMBomMatl, so now I need to figure that issue out… thank you Arthia and Amit!


amitr70
Semi-Pro I
Forum|alt.badge.img
  • Semi-Pro I
  • 30 replies
  • Answer
  • January 7, 2025

Two workarounds

  1. Try to add grouping if you are getting duplicate records
  2. Extract the working query from Traces screen. And tweak it according in a view. Use that view in your GI. Move all calculations to your SQL view. This will keep your GI clean and simple.

Forum|alt.badge.img
  • Author
  • Freshman II
  • 180 replies
  • January 7, 2025
arthia98 wrote:

Hi ​@swartzfeger , To resolve divide by zero error, add if condition like below

=IIf([SOLine.CuryLineAmt]>0, Round(CDbl(([SOLine.CuryLineAmt]-[AMProdItem.WIPTotal]) / [SOLine.CuryLineAmt]) * 100, 2),0)

=IIf([AMProdItem.WIPTotal]>0, Round(CDbl([SOLine.CuryLineAmt]/[AMProdItem.WIPTotal]),2),0)

 

Please check the table joins and fields to resolve duplicate issue

amitr70 wrote:

Two workarounds

  1. Try to add grouping if you are getting duplicate records
  2. Extract the working query from Traces screen. And tweak it according in a view. Use that view in your GI. Move all calculations to your SQL view. This will keep your GI clean and simple.

Ok, I solved it!

Originally, I had AMProdItem joined to AMBomMatl
bomID = bomID

I also needed to add:
revisionID = revisionID

Thank you both!


Michaelh
Semi-Pro III
Forum|alt.badge.img+2
  • Semi-Pro III
  • 192 replies
  • January 7, 2025

Just something to add, when you’re looking at a field using ALT+CLICK you should see a blue link to the table’s schema. In that schema you will see all the KEY FIELDS. This will help you ensure you’re joining properly and not getting duplicates. It isn’t perfect, but it keeps me going down the right path.


Forum|alt.badge.img
  • Author
  • Freshman II
  • 180 replies
  • January 7, 2025
Michaelh wrote:

Just something to add, when you’re looking at a field using ALT+CLICK you should see a blue link to the table’s schema. In that schema you will see all the KEY FIELDS. This will help you ensure you’re joining properly and not getting duplicates. It isn’t perfect, but it keeps me going down the right path.

Michael -- killer tip! I only think to add one key field but I will definitely use this in the future… thank you!


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings