<?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: arcpy.da.SearchCursor - sqlprefix in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/135#M55</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;As as an application and database devloper, it just makes sense to only return the rows needed rather than return everything and filter on the app side.&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Isn't the main way this is supported with ArcSDE databases using the &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000006r000000"&gt;Make Query Table&lt;/A&gt; tool? I think that's the only way to access fully native SQL queries off the geodatabase. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;arcpy.da.SearchCursor is a huge improvement. I've heard pandas is really really fast (especially for joins) so I'm hoping Esri is considering including it in their default python distro.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 11 Sep 2014 18:31:07 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2014-09-11T18:31:07Z</dc:date>
    <item>
      <title>arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/120#M40</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Does the sql prefix - DISTINCT work well on SDE tables.&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000" rel="nofollow"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Syntax SearchCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})&lt;/P&gt;&lt;P&gt;An SQL prefix clause is positioned in the first position and will be inserted between the SELECT keyword and the SELECT COLUMN LIST. The SQL prefix clause is most commonly used for clauses such as DISTINCT or ALL.&lt;/P&gt;&lt;P&gt;My following example is resulting in all rows, not distinct rows.&amp;nbsp; rs = arcpy.da.SearchCursor(sTableName, (sSelectField1, sSelectField2), sSqlExp, None, None, ("DISTINCT", sSqlPostfix)) &lt;/P&gt;&lt;OL&gt;&lt;LI&gt;What elese could I do to get truely distinct records only. &lt;/LI&gt;&lt;LI&gt;I store the values in a list and are they any function to get unique values in a list. Example [1,2,2,3,3,3,5,6,7] to [1,2,3,5,6,7]. Also set(xyz) won't work because I'm using a list, whereas set(xyz) works on tuples.&lt;/LI&gt;&lt;/OL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 Sep 2014 21:20:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/120#M40</guid>
      <dc:creator>ManikMohandas</dc:creator>
      <dc:date>2014-09-08T21:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/121#M41</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I doubt you can perform GROUP BY clause in any arcpy SearchCursor (I can't remember where I saw this, but I think this is the case).&amp;nbsp; However, you can:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Depending upon the Database type, you can issue your SQL in the python code.&amp;nbsp; You will need the appropriate library (cx_Oracle, pyodbc, pysmssql, etc).&lt;/P&gt;&lt;P&gt;2. These typically put results into a "cursor" (not an arcpy cursor).&amp;nbsp; So you will then have to parse this into your own list/array.&lt;/P&gt;&lt;P&gt;3. Take the list and either use it as it or perform an additional conversion to NumPy.&lt;/P&gt;&lt;P&gt;4. Or you can utilize pandas library to convert from your list and perform just about any grouping you'd want.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

&lt;SPAN class="kwd"&gt;import&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; cx_Oracle&lt;/SPAN&gt;


&lt;SPAN class="pln"&gt;import numpy&lt;/SPAN&gt;


&lt;SPAN class="pln"&gt;import pandas

con &lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; cx_Oracle&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;connect&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="str"&gt;'theuser'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; &lt;/SPAN&gt;&lt;SPAN class="str"&gt;'thepass'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; &lt;/SPAN&gt;&lt;SPAN class="str"&gt;'your DB alias on your TNSNAMES.ORA file '&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;
cur &lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; con&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;cursor&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;()&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;
&lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;if&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; cur&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;execute&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="str"&gt;'select fld1, fld2 from WHERE fld1 = 'blah' GROUP BY fld1, fld2'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;):&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;print&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; &lt;/SPAN&gt;&lt;SPAN class="str"&gt;"finally, it works!!!"&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;
&lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;else&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;:&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;print&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; &lt;/SPAN&gt;&lt;SPAN class="str"&gt;"facepalm"&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;&lt;/SPAN&gt;


&lt;SPAN class="str"&gt;&lt;/SPAN&gt; 


datArray = []&amp;nbsp; 


cxRows = cursor.fetchall()


for cxRow in cxRows:


&amp;nbsp;&amp;nbsp; datArray.append(cxRow)


con&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;close&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;()&lt;/SPAN&gt;


&lt;SPAN class="pun"&gt;&lt;/SPAN&gt; 


&lt;SPAN class="pun"&gt;#convert the array to a pandas DataFrame&lt;/SPAN&gt;


&lt;SPAN class="pun"&gt;dbDF = DataFrame(datArray, columns=['fld1', 'fld2'])&lt;/SPAN&gt;


&lt;SPAN class="pun"&gt;&lt;/SPAN&gt; 


&lt;SPAN class="pun"&gt;#to get unique values from any pandas datframe&lt;/SPAN&gt;


&lt;SPAN class="pun"&gt;uniquevals = numpy.unique(dbDF['fld1'])&lt;/SPAN&gt;

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:01:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/121#M41</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-10T20:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/122#M42</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I get error for these, because I dont have those modules:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN class="keyword"&gt;import cx_Oracle&amp;nbsp;&amp;nbsp; - &lt;SPAN class="keyword"&gt;Is there an equivalent for SQL Server, our SDE runs of it.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN class="keyword"&gt;import pandas&amp;nbsp; - Is it an open source freeware? Just to be sure.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 16:16:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/122#M42</guid>
      <dc:creator>ManikMohandas</dc:creator>
      <dc:date>2014-09-09T16:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/123#M43</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Some small example on creating a unique list of values in a field:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14102830943568124 jive_text_macro" jivemacro_uid="_14102830943568124"&gt;
&lt;P&gt;# create a unique list of the values in a field in a table or featureclass&lt;/P&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;FC_or_TBL = r"D:\Xander\Genesis\Tablas LayerRef\bk_DLLO_931.gdb\AG_LAYERREF"&lt;/P&gt;
&lt;P&gt;fld_name1 = "COLUMNA"&lt;/P&gt;
&lt;P&gt;unique_list = list(set(r[0] for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1))))&lt;/P&gt;
&lt;P&gt;unique_list.sort()&lt;/P&gt;
&lt;P&gt;print "\n".join(unique_list)&lt;/P&gt;
&lt;P&gt;print "\n#####\n"&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;On line 5 a list is created using list comprehensions, which is converted to a set (unique values) and parsed back to a list&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14102831596419720" jivemacro_uid="_14102831596419720"&gt;
&lt;P&gt;# using a combination of 2 columns&lt;/P&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;FC_or_TBL = r"D:\Xander\Genesis\Tablas LayerRef\bk_DLLO_931.gdb\AG_LAYERREF"&lt;/P&gt;
&lt;P&gt;fld_name1 = "COLUMNA"&lt;/P&gt;
&lt;P&gt;fld_name2 = "ID_LAYERREF"&lt;/P&gt;
&lt;P&gt;unique_list = list(set("{0},{1}".format(r[0], r[1]) for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2))))&lt;/P&gt;
&lt;P&gt;print "\n".join(unique_list)&lt;/P&gt;
&lt;P&gt;print "\n#####\n"&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;On line 6 the list is created from the combination of 2 fields&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14102832018461601 jive_text_macro" jivemacro_uid="_14102832018461601"&gt;
&lt;P&gt;# including a where clause&lt;/P&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;FC_or_TBL = r"D:\Xander\Genesis\Tablas LayerRef\bk_DLLO_931.gdb\AG_LAYERREF"&lt;/P&gt;
&lt;P&gt;fld_name1 = "COLUMNA"&lt;/P&gt;
&lt;P&gt;fld_name2 = "ID_LAYERREF"&lt;/P&gt;
&lt;P&gt;fld_search = "COLUMNA"&lt;/P&gt;
&lt;P&gt;search_value = "OBJECTID"&lt;/P&gt;
&lt;P&gt;where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(FC_or_TBL, fld_search), search_value)&lt;/P&gt;
&lt;P&gt;unique_list = list(set("{0}_{1}".format(r[0], r[1]) for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where)))&lt;/P&gt;
&lt;P&gt;print "\n".join(unique_list)&lt;/P&gt;
&lt;P&gt;print "\n#####\n"&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In this case an additional where clause is used to limited the results. As James already showed, Pandas is a powerful library. If you have it, use it. If you don't and you don't want to install it, the above might be an alternative.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xander&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 17:21:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/123#M43</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2014-09-09T17:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/124#M44</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looks like pyodbc will connect to SQL Server &lt;A href="http://stackoverflow.com/questions/7317195/mssql-in-python-2-7" title="http://stackoverflow.com/questions/7317195/mssql-in-python-2-7"&gt;sql server - MSSQL in python 2.7 - Stack Overflow&lt;/A&gt; &lt;/P&gt;&lt;P&gt;Pandas home &lt;A href="http://pandas.pydata.org/" title="http://pandas.pydata.org/"&gt;Python Data Analysis Library — pandas: Python Data Analysis Library&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am just a developer and don't have experience getting these installed, so you will have to figure that out.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 17:22:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/124#M44</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-09T17:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/125#M45</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the example Xander --- it's good to see straight-forward solutions.&amp;nbsp; While it's frustrating to be unable to issue more complex SQL on arcpy/GIS side of things, I'd even prefer to completely remove the SQL out of the code I posted and move that to StoredProcedures/PL-SQL packages on the database instead. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As as an application and database devloper, it just makes sense to only return the rows needed rather than return everything and filter on the app side.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 17:26:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/125#M45</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-09T17:26:32Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/126#M46</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is a simple sql clause that I use to get an ordered unique list of values from the Speed field in the SpeedLimits table. Distinct only returns unique values, Order by with ASC for ascending, sorts the rows.&amp;nbsp; Note my Speed field is a test type and not numeric.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;uniqueList = []&lt;/P&gt;&lt;P&gt;with arcpy.da.SearchCursor("SpeedLimits", ["Speed"], sql_clause=("DISTINCT", 'ORDER BY Speed ASC') ) as rows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uniqueList.append(row[0])&lt;/P&gt;&lt;P&gt;stringFilter.list = uniqueList&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 17:33:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/126#M46</guid>
      <dc:creator>RobertBurke</dc:creator>
      <dc:date>2014-09-09T17:33:51Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/127#M47</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wowww. Does "DISTINCT" work for you? If so what version of ArcMap are you using?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The whole problem started since use sqlprefix didn't work with arcpy.da.SearchCursor. It won't throw an error but wouldn't give the correct distinct recrods. So what do you mean by "Speed field is a test type and not numeric". Does python have a data type "test".&amp;nbsp; Just to rant, python could use better terminology for naming their data types - arrays(can they hold strings?), list, set, dictionary, tuples...... Ofcourse, I'm not an expert but its totally confusing.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 17:44:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/127#M47</guid>
      <dc:creator>ManikMohandas</dc:creator>
      <dc:date>2014-09-09T17:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/128#M48</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It works for Robert because he is only returning 1 field.&amp;nbsp; Your OP has multiple fields -- how can you return distinct values for multiple fields without correctly apply a GROUP BY clause?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 18:45:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/128#M48</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-09T18:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/129#M49</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry Manny, Test should be Text.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a field to store speed limit values, 45, 55, 65 etc.&amp;nbsp; I was just trying to point out, that my field called 'Speed' is defined as the Text data type, and not as a Numeric data type as some might expect.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And I also only have the one field.&amp;nbsp; I haven't tried Two.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 19:28:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/129#M49</guid>
      <dc:creator>RobertBurke</dc:creator>
      <dc:date>2014-09-09T19:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/130#M50</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;@Xander Bakker&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What does this do: set(&lt;SPAN class="string"&gt;"{0}_{1}"&lt;/SPAN&gt;.format(r[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;], r[&lt;SPAN class="number"&gt;1&lt;/SPAN&gt;]) . Specifically, the curly brackets {} and the need for format(). Is format() mandatory?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Is it possible to breakdown the following into more "bite sized", multi-line code:&lt;/P&gt;&lt;P&gt;unique_list = list(set(&lt;SPAN class="string"&gt;"{0}_{1}".format(r[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;], r[&lt;SPAN class="number"&gt;1&lt;/SPAN&gt;]) &lt;SPAN class="keyword"&gt;for&lt;/SPAN&gt; r &lt;SPAN class="keyword"&gt;in&lt;/SPAN&gt; arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where))) &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Also, what is the difference between set() and list() do. Can't you do an app&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 19:45:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/130#M50</guid>
      <dc:creator>ManikMohandas</dc:creator>
      <dc:date>2014-09-09T19:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/131#M51</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks James and Robert.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Sep 2014 19:47:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/131#M51</guid>
      <dc:creator>ManikMohandas</dc:creator>
      <dc:date>2014-09-09T19:47:19Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/132#M52</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Manny,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My explanation was a little short, sorry for that... and creating these type of "one-liners" doesn't allow for easy interpretation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let's break it down into pieces:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_1410313723827331 jive_text_macro" jivemacro_uid="_1410313723827331"&gt;
&lt;P&gt;# let's say we have a list with duplicate values&lt;/P&gt;
&lt;P&gt;list1 = [1, 3, 5, 7, 3, 2, 5, 6, 8, 7, 5, 4, 3, 2, 6, 2]&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# the set() is used to convert it to a set of unique values&lt;/P&gt;
&lt;P&gt;set1 = set(list1)&lt;/P&gt;
&lt;P&gt;print set1&lt;/P&gt;
&lt;P&gt;# returns: set([1, 2, 3, 4, 5, 6, 7, 8])&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# if you want to convert it to a list again (to loop over it)&lt;/P&gt;
&lt;P&gt;list2 = list(set1)&lt;/P&gt;
&lt;P&gt;print list2&lt;/P&gt;
&lt;P&gt;# returns [1, 2, 3, 4, 5, 6, 7, 8]&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A set is very powerful to get a unique list of values and combining sets allows you to obtain values that are in one list but not in the other and info like this. More on this in my post: &lt;A href="https://community.esri.com/docs/DOC-1927"&gt;Some Python Snippets&lt;/A&gt; (scroll down to comparing lists)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To create the list some list comprehensions are used (check out this document I posted: &lt;A href="https://community.esri.com/docs/DOC-1975"&gt;Using Python List Comprehensions&lt;/A&gt; ).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let's break down the code:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14103140670945763" jivemacro_uid="_14103140670945763" modifiedtitle="true"&gt;
&lt;P&gt;unique_list = list(set(&lt;SPAN class="string"&gt;"{0}_{1}".format(r[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;], r[&lt;SPAN class="number"&gt;1&lt;/SPAN&gt;]) &lt;SPAN class="keyword"&gt;for&lt;/SPAN&gt; r &lt;SPAN class="keyword"&gt;in&lt;/SPAN&gt; arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where))) &lt;/SPAN&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The set() and list() at the beginning of the line have been explained above.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Within the brackets of the set() command a lot is going on. Let's not start from left to right, but with the SearchCursor. It takes a table or featureclass, a tuple (or list) of field names and in this case a where clause. This is pretty standard and I don't think needs much explanation. Normally when you use a search cursor you obtain a cursor object which you use to loop through each row (or feature) in the cursor. Next you probably access the fields in each row and do something with it like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14103145228519786" jivemacro_uid="_14103145228519786"&gt;
&lt;P&gt;with arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where) as curs:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val_fld1 = row[0]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # etc&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In case of the list comprehension this is done on one line. It retrieves each row "r" in the SeachCursor and returns this:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14103146725619422 jive_text_macro" jivemacro_uid="_14103146725619422"&gt;
&lt;P&gt;"{0}_{1}".format(r[0], r[1])&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;string.format() allows you to create a string and pass in variables. It is a pretty need way to combine values and string in a certain format&lt;/P&gt;&lt;P&gt;r[0] refers to the value of fld_name1&lt;/P&gt;&lt;P&gt;r[1] refers to the value of fld_name2&lt;/P&gt;&lt;P&gt;{0} refers to the first parameter specified in the format command&lt;/P&gt;&lt;P&gt;{1} refers to the second parameter specified in the format command&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;... so it the value for fld_name1 is "&lt;STRONG&gt;abc&lt;/STRONG&gt;" and the value in fld_name2 would be &lt;STRONG&gt;1&lt;/STRONG&gt;, this would result in the string "&lt;STRONG&gt;abc_1&lt;/STRONG&gt;".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I used something similar for creating the where clause:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14103151018524837 jive_text_macro" jivemacro_uid="_14103151018524837"&gt;
&lt;P&gt;where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(FC_or_TBL, fld_search), search_value)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It starts with a string "{0} = '{1}'". The {0} is replaced with the first argument of the format function:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_1410315176767540 jive_text_macro" jivemacro_uid="_1410315176767540"&gt;
&lt;P&gt;arcpy.AddFieldDelimiters(FC_or_TBL, fld_search)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will put quotes or brackets or nothing around the field name based on the type of workspace (that can be useful if you don't know if your input is a shapefiles, personal, file or enterprise geodatabase...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The {1} is replace with the value in the variable search_value. Please note that this where clause will fail if your field is not of type TEXT (string). It is possible to extend this functionality and detect the columns type, but for simplicity reasons (read &lt;EM&gt;I was to lazy&lt;/EM&gt;) I did not do that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you have any other doubt, please let me know...&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xander&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Sep 2014 02:17:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/132#M52</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2014-09-10T02:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/133#M53</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have used arcpy.ArcSDESQLExecute with great succes when it comes to complex queries involving DISTINCT and GROUP BY. It also seems to perform pretty well as an alternative to using a cursor and doing the grouping in the loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Carsten&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 09:45:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/133#M53</guid>
      <dc:creator>CarstenBøcker1</dc:creator>
      <dc:date>2014-09-11T09:45:49Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/134#M54</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you post an example?&amp;nbsp; I remember not being able to correctly apply a GROUP BY with multiple fields present in the SQL statement but it has been a while since then and I am not sure if I can even locate the thread in this new place.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 12:54:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/134#M54</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-11T12:54:21Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/135#M55</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;As as an application and database devloper, it just makes sense to only return the rows needed rather than return everything and filter on the app side.&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Isn't the main way this is supported with ArcSDE databases using the &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000006r000000"&gt;Make Query Table&lt;/A&gt; tool? I think that's the only way to access fully native SQL queries off the geodatabase. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;arcpy.da.SearchCursor is a huge improvement. I've heard pandas is really really fast (especially for joins) so I'm hoping Esri is considering including it in their default python distro.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 18:31:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/135#M55</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-11T18:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/136#M56</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I can't seem to locate the exact thread I participated in but it was about attempting to setup a Make Query Table with multiple fields and include a GROUP BY statement in the SQL.&amp;nbsp; It wouldn't work from what I can remember, or it would execute but it doesn't do the grouping as expected.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Perhaps I am misunderstanding the QueryTable! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 18:47:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/136#M56</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-11T18:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/137#M57</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3100"&gt;Xander Bakker&lt;/A&gt;‌. Thank you so very much. That was a good primer on data access, lists, set and string functions. Yes, I was able to work around the limitations of SQL prefix like DISTINCT not working on multiple fields through arcpy.da.SearchCursor. Wish ESRI would further improve "DA".&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 22:19:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/137#M57</guid>
      <dc:creator>ManikMohandas</dc:creator>
      <dc:date>2014-09-11T22:19:10Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/138#M58</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So... are you confirming that what I mentioned is not possible with Make Query Table?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There's some confusion that this is possible with multiple fields returned.&amp;nbsp; However, I just don't see how so because a correct SQL statement that returns DISTINCT values on multiple fields simply must have a GROUP BY clause in it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 12 Sep 2014 13:01:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/138#M58</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-12T13:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor - sqlprefix</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/139#M59</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Curtis,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am using Pandas quite extensively and I can confirm it offers a lot of value for table/list operations!&amp;nbsp; However, in the context of this thread, it just doesn't make sense because the idea of issuing SQL against the database is to return only those rose that meet the query.&amp;nbsp; That is, I wouldn't want to return 1 million rows just to process them with Pandas functions.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 12 Sep 2014 13:04:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-sqlprefix/m-p/139#M59</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-12T13:04:36Z</dc:date>
    </item>
  </channel>
</rss>

