<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Pass sql expression with arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540913#M42266</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with Dan, not sure what the search cursor is doing here.&amp;nbsp; You cursor is getting a shape but you aren't doing anything with the shape.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How about you step back from the code and explain what you are trying to achieve.&amp;nbsp; There may be a different way of going about it.&amp;nbsp; If you can explain your data structures a bit, even better.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 09 Apr 2020 18:21:43 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2020-04-09T18:21:43Z</dc:date>
    <item>
      <title>Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540908#M42261</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am try to select the polygons with the max area within parcels but i am having trouble passing the sql through &lt;SPAN&gt;arcpy.da.SearchCursor&lt;/SPAN&gt;. How can i pass this sql through SearchCursor?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sql = Shape_Area in (select max(Shape_Area) from Bldg_FP1 group by PIN)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;with arcpy.da.SearchCursor(fc2, ['SHAPE@']) as cursor:
     for row in cursor:
          arcpy.SelectLayerByAttribute_management(fc,"NEW_SELECTION", sql)‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:27:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540908#M42261</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2021-12-11T23:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540909#M42262</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;are you using arcgis pro or arc map?&lt;/P&gt;&lt;P&gt;Pro's help&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/tool-reference/data-management/select-layer-by-location.htm" title="https://pro.arcgis.com/en/pro-app/tool-reference/data-management/select-layer-by-location.htm"&gt;Select Layer By Location—Data Management toolbox | Documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2020 17:36:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540909#M42262</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2020-04-09T17:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540910#M42263</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My bad, Arcmap and would be using &lt;SPAN&gt;arcpy.SelectLayerByAttribute_management&lt;/SPAN&gt; not ByLocation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have tried the following I don't get an error but nothing is selected&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;sql = "select MAX(Shape_Area) from fc group by PIN"
with arcpy.da.SearchCursor(fc2, ['SHAPE@']) as cursor:
     for row in cursor:
          arcpy.SelectLayerByAttribute_management(fc2,"NEW_SELECTION", sql)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:27:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540910#M42263</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2021-12-11T23:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540911#M42264</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;not sure why you would be running it through a searchcursor in the first place when the selectbyattribute would work on the whole of fc2&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2020 18:07:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540911#M42264</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2020-04-09T18:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540912#M42265</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;EM&gt;I have tried the following I don't get an error but nothing is selected&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Are you running this against a feature class or have you applied &lt;A href="https://pro.arcgis.com/en/pro-app/tool-reference/data-management/make-feature-layer.htm"&gt;MakeFeatureLayer()&lt;/A&gt;&amp;nbsp;such that fc2 = yourFeatureLayer&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Take a look at this:&amp;nbsp;&amp;nbsp;&lt;A class="link-titled" href="https://gis.stackexchange.com/questions/264051/selecting-maximum-value-in-attribute-table-using-python-code" title="https://gis.stackexchange.com/questions/264051/selecting-maximum-value-in-attribute-table-using-python-code"&gt;arcpy - Selecting maximum value in attribute table using Python code? - Geographic Information Systems Stack Exchange&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2020 18:19:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540912#M42265</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-04-09T18:19:04Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540913#M42266</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with Dan, not sure what the search cursor is doing here.&amp;nbsp; You cursor is getting a shape but you aren't doing anything with the shape.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How about you step back from the code and explain what you are trying to achieve.&amp;nbsp; There may be a different way of going about it.&amp;nbsp; If you can explain your data structures a bit, even better.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2020 18:21:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540913#M42266</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-04-09T18:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540914#M42267</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I was able to get to select features with the following but i was trying to use the &lt;SPAN&gt;arcpy.da.SearchCursor&amp;nbsp;&lt;/SPAN&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;sql = "[Shape_Area] in (SELECT max([Shape_Area]) FROM fc2 GROUP BY [PIN])"
arcpy.SelectLayerByAttribute_management(fc2,"NEW_SELECTION", sql)‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:27:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540914#M42267</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2021-12-11T23:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540915#M42268</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the searchcursor isn't necessary&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2020 19:05:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540915#M42268</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2020-04-09T19:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Pass sql expression with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540916#M42269</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That SQL clause is fraught for failure. An IN comparison on a floating-point value is a recipe for random results -- You could cross-select features with areas that just happen to share a parcel size but don't meet the intended attribute constraint and you might miss intended features due to representation issues.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In general, placing&amp;nbsp;the&amp;nbsp;SelectLayerByAttribute inside&amp;nbsp;the&amp;nbsp;cursor iteration is an anti-pattern (because you&amp;nbsp;alter the layer on which the cursor is based), but in this case, it's also unnecessary (because you wanted to restrict the layer before cursor execution would begin).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If your dataset supports the full SQL implementation (not file geodatabase or Access), you can use the ROW_NUMBER window function to assign rank&amp;nbsp;in a virtual table, then select parcel identifiers associated with the first-ranked features.&amp;nbsp; In PostgreSQL the full query could look like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;SELECT&lt;/SPAN&gt;	objectid&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
      	continent &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; continent_name&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
      	name &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; country_name
&lt;SPAN class="keyword token"&gt;FROM&lt;/SPAN&gt;	country_gen
&lt;SPAN class="keyword token"&gt;WHERE&lt;/SPAN&gt;	objectid &lt;SPAN class="operator token"&gt;in&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;
		&lt;SPAN class="keyword token"&gt;SELECT&lt;/SPAN&gt; 	objectid
		&lt;SPAN class="keyword token"&gt;FROM&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;
			&lt;SPAN class="keyword token"&gt;SELECT&lt;/SPAN&gt;	objectid&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; 
			      	continent&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; 
			      	&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row_number&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;over&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;
				        	&lt;SPAN class="keyword token"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;BY&lt;/SPAN&gt; continent
				        	&lt;SPAN class="keyword token"&gt;ORDER&lt;/SPAN&gt; 	  &lt;SPAN class="keyword token"&gt;BY&lt;/SPAN&gt; ST_Area&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;shape::geography&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;DESC&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
			      	&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;::&lt;SPAN class="keyword token"&gt;int&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; group_rank
			&lt;SPAN class="keyword token"&gt;FROM&lt;/SPAN&gt;    country_gen
		&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; vt
		&lt;SPAN class="keyword token"&gt;WHERE&lt;/SPAN&gt; 	group_rank &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;
	&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;ORDER&lt;/SPAN&gt; 	&lt;SPAN class="keyword token"&gt;BY&lt;/SPAN&gt; ST_Area&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;shape::geography&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;DESC&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the output of which is:&lt;/P&gt;&lt;TABLE class="j-table jiveBorder" style="border: 1px solid #c6c6c6; width: 61.5327%;"&gt;&lt;THEAD&gt;&lt;TR style="background-color: #efefef; height: 25px;"&gt;&lt;TH style="width: 13.9194%; height: 25px;"&gt;objectid&lt;/TH&gt;&lt;TH style="width: 41.7582%; height: 25px;"&gt;continent_name&lt;/TH&gt;&lt;TH style="width: 40.293%; height: 25px;"&gt;country_name&lt;/TH&gt;&lt;/TR&gt;&lt;/THEAD&gt;&lt;TBODY&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;253&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'Asia'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Russian Federation'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;29&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'Antarctica'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Antarctica'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;18&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'North America'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Canada'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;25&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'South America'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Brazil'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;223&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'Australia'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Australia'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;105&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'Africa'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Congo DRC'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;196&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'Europe'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'Ukraine'&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 13.9194%; height: 25px;"&gt;99&lt;/TD&gt;&lt;TD style="width: 41.7582%; height: 25px;"&gt;'Oceania'&lt;/TD&gt;&lt;TD style="width: 40.293%; height: 25px;"&gt;'New Zealand'&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You'd only want lines 5-17 in your WHERE clause, suitably altered for your data.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:28:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-sql-expression-with-arcpy/m-p/540916#M42269</guid>
      <dc:creator>VinceAngelo</dc:creator>
      <dc:date>2021-12-11T23:28:02Z</dc:date>
    </item>
  </channel>
</rss>

