<?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: Making more Complex SQL Queries with Arcpy SearchCursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62406#M5035</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In addition to the SQL reference that Dan links to, make sure to read &lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm" title="https://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm"&gt;SearchCursor—Data Access module | Documentation&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In order to use SQL SELECT criteria, like DISTINCT, with ArcPy DA cursors, you will need to use a &lt;SPAN style="font-family: courier new, courier, monospace;"&gt;sql_clause&lt;/SPAN&gt; argument to define what Esri calls a "SQL prefix."&amp;nbsp; In general, and particularly with file geodatabases, SQL SELECT criteria do &lt;SPAN style="text-decoration: underline;"&gt;not&lt;/SPAN&gt; work with spatial data types.&amp;nbsp; For example you cannot write something like, &lt;SPAN style="font-family: courier new, courier, monospace;"&gt;SELECT DISTINCT shape.&amp;nbsp; &lt;/SPAN&gt;If you want to compare spatial data, you will need to create another column(s) and put a spatial property in it, e.g., create a WKT field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assuming your four fields are text (pnt being some text-based property or representation of your point data), your where clause would be:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;where_clause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NOT(name IS NULL OR corridor IS NULL OR pnt IS NULL OR zone IS NULL)"&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and your SQL clause would be:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;sql_clause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"DISTINCT"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; None&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and your cursor definition would look like:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;gdbConnectTable&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"name"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"corridor"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"pnt"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"zone"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; where_clause&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;where_clause&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; sql_clause&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;sql_clause&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 22 Mar 2020 17:22:03 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2020-03-22T17:22:03Z</dc:date>
    <item>
      <title>Making more Complex SQL Queries with Arcpy SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62403#M5032</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I am writing a python script and want to perform the following query using arcpy's SearchCursor:&lt;/P&gt;&lt;P&gt;select unique name, corridor, pnt, zone where not (name is null or corridor is null or pnt is null or zone is null)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I did try making the SearchCursor's where_clause =&amp;nbsp;&lt;SPAN&gt;(name is null or corridor is null or pnt is null or zone is null), but the script could not run like that and gave an error. I am using the following code to query the arcgis data successfully but not getting the data the way I need it:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;with arcpy.da.SearchCursor(gdbConnectTable, fieldsList) as readCursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; for record in readCursor:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have not been able to find any documentation on getting SearchCursor to yield unique results.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is it possible to have the arcpy SearchCursor return what I need?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 21 Mar 2020 23:03:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62403#M5032</guid>
      <dc:creator>LaurenWallace</dc:creator>
      <dc:date>2020-03-21T23:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Making more Complex SQL Queries with Arcpy SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62404#M5033</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;text case is important&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm" title="https://pro.arcgis.com/en/pro-app/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm"&gt;SQL reference for query expressions used in ArcGIS—ArcGIS Pro | Documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 21 Mar 2020 23:55:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62404#M5033</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2020-03-21T23:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Making more Complex SQL Queries with Arcpy SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62405#M5034</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A useful workflow for me is to write the expression first in the 'Select by attribute' window and see if it returns the expected results, then enclose the expression in quotations.&lt;/P&gt;&lt;P&gt;for example a simple selection&amp;nbsp;&lt;/P&gt;&lt;P&gt;"Id" = 0&lt;/P&gt;&lt;P&gt;becomes&lt;/P&gt;&lt;P&gt;'"id" = 0'&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 22 Mar 2020 14:17:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62405#M5034</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2020-03-22T14:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: Making more Complex SQL Queries with Arcpy SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62406#M5035</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In addition to the SQL reference that Dan links to, make sure to read &lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm" title="https://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm"&gt;SearchCursor—Data Access module | Documentation&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In order to use SQL SELECT criteria, like DISTINCT, with ArcPy DA cursors, you will need to use a &lt;SPAN style="font-family: courier new, courier, monospace;"&gt;sql_clause&lt;/SPAN&gt; argument to define what Esri calls a "SQL prefix."&amp;nbsp; In general, and particularly with file geodatabases, SQL SELECT criteria do &lt;SPAN style="text-decoration: underline;"&gt;not&lt;/SPAN&gt; work with spatial data types.&amp;nbsp; For example you cannot write something like, &lt;SPAN style="font-family: courier new, courier, monospace;"&gt;SELECT DISTINCT shape.&amp;nbsp; &lt;/SPAN&gt;If you want to compare spatial data, you will need to create another column(s) and put a spatial property in it, e.g., create a WKT field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assuming your four fields are text (pnt being some text-based property or representation of your point data), your where clause would be:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;where_clause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NOT(name IS NULL OR corridor IS NULL OR pnt IS NULL OR zone IS NULL)"&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and your SQL clause would be:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;sql_clause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"DISTINCT"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; None&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and your cursor definition would look like:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;gdbConnectTable&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"name"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"corridor"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"pnt"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"zone"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; where_clause&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;where_clause&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; sql_clause&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;sql_clause&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 22 Mar 2020 17:22:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/making-more-complex-sql-queries-with-arcpy/m-p/62406#M5035</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-03-22T17:22:03Z</dc:date>
    </item>
  </channel>
</rss>

