<?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 How to get the Counts when using Group By in a Search Cursor in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1096552#M6610</link>
    <description>&lt;P&gt;I dug through a lot of posts but not getting it.&amp;nbsp; I normally use Summary Stats but cannot in this case.&amp;nbsp; Python 2.7.&lt;/P&gt;&lt;P&gt;Using Group By in SQL looks something like&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DougBrowning_0-1631136962433.png" style="width: 474px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/22634i49738C5319F6D807/image-dimensions/474x161?v=v2" width="474" height="161" role="button" title="DougBrowning_0-1631136962433.png" alt="DougBrowning_0-1631136962433.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;How do I get the count as a field so I can use it?&amp;nbsp; Most SQL examples have count(field) as field but I can't get that to go and not really sure where to put it.&amp;nbsp; I got the trick to add ObjectID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get a total for each EvaluationID.&lt;/P&gt;&lt;P&gt;Here is what I got&amp;nbsp; (note row[0] is from a upperlevel searchcursor)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;selectClause = row[0] + " IS NOT NULL or CAST(" + row[0] + " AS CHARACTER(255)) &amp;lt;&amp;gt; ''"
fieldList2 = ["EvaluationID", "ObjectID", "COUNT(EvaluationID) AS idCount"]
with arcpy.da.SearchCursor(table, fieldList2, selectClause, sql_clause=(None, 'GROUP BY EvaluationID, ObjectID')) as cursor2:
    for row2 in cursor2:
         print "use count here somehow"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the error&amp;nbsp;An expected Field was not found or could not be retrieved properly. [idCount]&lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
    <pubDate>Wed, 08 Sep 2021 22:07:48 GMT</pubDate>
    <dc:creator>DougBrowning</dc:creator>
    <dc:date>2021-09-08T22:07:48Z</dc:date>
    <item>
      <title>How to get the Counts when using Group By in a Search Cursor</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1096552#M6610</link>
      <description>&lt;P&gt;I dug through a lot of posts but not getting it.&amp;nbsp; I normally use Summary Stats but cannot in this case.&amp;nbsp; Python 2.7.&lt;/P&gt;&lt;P&gt;Using Group By in SQL looks something like&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DougBrowning_0-1631136962433.png" style="width: 474px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/22634i49738C5319F6D807/image-dimensions/474x161?v=v2" width="474" height="161" role="button" title="DougBrowning_0-1631136962433.png" alt="DougBrowning_0-1631136962433.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;How do I get the count as a field so I can use it?&amp;nbsp; Most SQL examples have count(field) as field but I can't get that to go and not really sure where to put it.&amp;nbsp; I got the trick to add ObjectID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get a total for each EvaluationID.&lt;/P&gt;&lt;P&gt;Here is what I got&amp;nbsp; (note row[0] is from a upperlevel searchcursor)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;selectClause = row[0] + " IS NOT NULL or CAST(" + row[0] + " AS CHARACTER(255)) &amp;lt;&amp;gt; ''"
fieldList2 = ["EvaluationID", "ObjectID", "COUNT(EvaluationID) AS idCount"]
with arcpy.da.SearchCursor(table, fieldList2, selectClause, sql_clause=(None, 'GROUP BY EvaluationID, ObjectID')) as cursor2:
    for row2 in cursor2:
         print "use count here somehow"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the error&amp;nbsp;An expected Field was not found or could not be retrieved properly. [idCount]&lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 22:07:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1096552#M6610</guid>
      <dc:creator>DougBrowning</dc:creator>
      <dc:date>2021-09-08T22:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the Counts when using Group By in a Search Cursor</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1096628#M6614</link>
      <description>&lt;P&gt;Hmm, couldn't get it to work, either.&lt;/P&gt;&lt;P&gt;Possible workarounds:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;use the query in &lt;A href="https://pro.arcgis.com/de/pro-app/latest/tool-reference/data-management/make-query-layer.htm" target="_blank" rel="noopener"&gt;the MakeQueryLayer tool&lt;/A&gt; and search its output&lt;/LI&gt;&lt;LI&gt;do it in python:&lt;/LI&gt;&lt;/OL&gt;&lt;LI-CODE lang="python"&gt;selectClause = row[0] + " IS NOT NULL or CAST(" + row[0] + " AS CHARACTER(255)) &amp;lt;&amp;gt; ''"
fieldList2 = ["EvaluationID", "ObjectID"]
tableData = [row for row in arcpy.da.SearchCursor(table, fieldList2, selectClause)]
uniqueEvaluationIDs = {td[0] for td in tableData}
for evaluationID in uniqueEvaluationIDs:
    count = len([td for td in tableData if td[0] == evaluationID])
    print "count of {}: {}".format(evaluationID, count)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 09 Sep 2021 05:40:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1096628#M6614</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-09T05:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the Counts when using Group By in a Search Cursor</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1098483#M6639</link>
      <description>&lt;P&gt;Thanks I am trying to avoid all the lopping but that was my backup plan.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Seems weird to have Group by in there but no way to actually use it.&amp;nbsp; But I find a lot of the SQL is only partially implemented.&lt;/P&gt;&lt;P&gt;Still hoping someone knows how.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 15:05:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/how-to-get-the-counts-when-using-group-by-in-a/m-p/1098483#M6639</guid>
      <dc:creator>DougBrowning</dc:creator>
      <dc:date>2021-09-15T15:05:28Z</dc:date>
    </item>
  </channel>
</rss>

