FeatureLayer Calculate SQL Syntax

403
2
Jump to solution
11-18-2021 12:33 PM
mapmcburney
New Contributor III

Hello I have a Feature Layer with three integer fields (A, B, and C)

I want to calculate the percentage between A and B. The following will run but I only get values of 100 or 0. Nothing in between, and manual calculations prove that there are valid values between 100 and 0.

overlay_result_layer.calculate(where= "1=1",calc_expression={"field": "C", "sqlExpression":"(B/A)*100"})
 
Can anyone see what is wrong with my syntax?
 
For additional info, I run the calculation below right before without errors perfectly fine;
 
overlay_result_layer.calculate(where= "1=1",calc_expression={"field": "B", "sqlExpression": "D * 247.105"})
 

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

It's the integers. Take your first expression, (B/A) * 100. Since B/A evaluates first, you're stuck with whatever rounded integer value results from the division. That is, 0 or 1, which then multiplied by 100 gives you 0 and 100, respectively.

Try instead, (B*100)/A. By multiplying your integer first, you avoid truncating the divided value as drastically.

- Josh Carlson
Kendall County GIS

View solution in original post

2 Replies
jcarlson
MVP Esteemed Contributor

It's the integers. Take your first expression, (B/A) * 100. Since B/A evaluates first, you're stuck with whatever rounded integer value results from the division. That is, 0 or 1, which then multiplied by 100 gives you 0 and 100, respectively.

Try instead, (B*100)/A. By multiplying your integer first, you avoid truncating the divided value as drastically.

- Josh Carlson
Kendall County GIS
mapmcburney
New Contributor III

You are a legend! Thank you!

0 Kudos