<?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: Select maximum values in Select By Attributes (greatest n per group) in Geodatabase Questions</title>
    <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369956#M8772</link>
    <description>&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I'm really struggling with subqueries in these FGDB views.&amp;nbsp; They seems temperamental with warnings about 'cannot be edited in clause mode' when I'm not in clause mode&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;My understanding is that clause mode is the non-SQL mode. I think it's the mode where you construct a query using the UI pick lists.&lt;/P&gt;&lt;P&gt;If I'm writing a complex SQL query in SQL mode, then it makes sense to get the message, "The expression can't be edited in clause mode", since that complex SQL wouldn't be parsable into the UI pick lists. I just ignore that message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you seeing something different?&lt;/P&gt;</description>
    <pubDate>Mon, 15 Jan 2024 00:00:58 GMT</pubDate>
    <dc:creator>Bud</dc:creator>
    <dc:date>2024-01-15T00:00:58Z</dc:date>
    <item>
      <title>Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369377#M8757</link>
      <description>&lt;P&gt;&lt;EM&gt;ArcGIS Pro 3.2.1; mobile geodatabase&lt;BR /&gt;&lt;BR /&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I'm trying to learn about different ways to select the greatest n per group in ArcGIS Pro. I came across this old ArcMap article that talks about a possible technique:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;A href="https://support.esri.com/en-us/knowledge-base/how-to-select-minimum-and-maximum-values-in-the-select-000008936" target="_self"&gt;Select minimum and maximum values in the Select By Attributes window in ArcMap&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Use GROUP BY subquery clauses to return values from aggregate functions on sets of values.&lt;/P&gt;&lt;P&gt;Example #3:&lt;/P&gt;&lt;P&gt;Select the records from the table with the most recent date &lt;STRONG&gt;for sets of values&lt;/STRONG&gt;, where &amp;lt;group_ID&amp;gt; is the field that contains the values that define the groups:&lt;/P&gt;&lt;PRE&gt;&amp;lt;date&amp;gt; = (SELECT MAX(date) FROM &amp;lt;table_name&amp;gt; GROUP BY &amp;lt;group_ID&amp;gt;)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I'll use the SQL expression above to try to select the latest row per species group. The result looks like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_2-1705026308386.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91357i4FEA834CFCEA0FBD/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_2-1705026308386.png" alt="Bud_2-1705026308386.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;t_date = (
    select 
        max(t_date) 
    from 
        species_records
    group by
        t_species)&lt;/PRE&gt;&lt;P&gt;That's not the result I was looking for.&lt;/P&gt;&lt;P&gt;This would be the result I'm looking for:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_0-1705060479030.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91415i0583DC61B09D8AFB/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_0-1705060479030.png" alt="Bud_0-1705060479030.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;t_date in (
    select
        max(t_date)
    from
        species_records s2 
&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;    where
        s2.t_species = species_records.t_species)&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;What am I missing in that article?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Notes:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I'm aware that there will be duplicate rows selected per group if there are multiple rows per species that have the same date. Breaking the tie (arbitrarily) would be ideal. But I don't know if that's possible with this particular approach.&lt;/LI&gt;&lt;LI&gt;I've attached sample Excel data that can be loaded into a geodatabase.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jan 2024 01:15:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369377#M8757</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-13T01:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369585#M8758</link>
      <description>&lt;P&gt;Wow, whoever wrote that Support article needs to brush up on their SQL.&amp;nbsp; The logic of that SQL query is flawed because it could select multiple records for a given set if a set had a non-maximal date that happened to be a maximal date for another set.&amp;nbsp; Usually when people are selecting maximum per group they want only the maximum selected (or multiple if maximum is shared), and not the maximum and maybe some others as well.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 15:31:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369585#M8758</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-12T15:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369676#M8759</link>
      <description>&lt;P&gt;I'm really struggling with subqueries in these FGDB views.&amp;nbsp; They seems temperamental with warnings about 'cannot be edited in clause mode' when I'm not in clause mode, or working then not working on a whim.&amp;nbsp; I thought I understood the limitation was only scalar results could be returned by the subquery, but&amp;nbsp; then I see otherwise.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 17:20:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369676#M8759</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2024-01-12T17:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369734#M8760</link>
      <description>&lt;P&gt;Yes, I’ve been struggling with FGDB views as well. I talk about the issues I’ve been having in my reply here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/make-query-table-use-date-field-in-join/idc-p/1369460/highlight/true#M27992" target="_self"&gt;Make Query Table — Use date field in join&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Summary: Creating a “view on a view” (joining on multiple fields) in an attempt to get around the “no correlated subqueries” limitation didn’t work for me. Views on views don’t seem to work.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 18:57:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369734#M8760</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-12T18:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369748#M8761</link>
      <description>&lt;P&gt;One thing that has helped me when writing FGDB SQL is to do a parallel test in a different geodatabase type where I can use a SQL client, such as DBeaver for mobile GDBs or SQL Developer for Oracle EGDBs. I put exactly the same data in a mobile GDB, mock up the SQL in DBeaver, then try to get something similar working in FGDB SQL.&lt;/P&gt;&lt;P&gt;It really helps me to write SQL in a SQL client that has proper error messages; it lets me make sure my syntax is correct in a normal environment first. FGDB SQL is challenging enough as it is; typos on my part make it so much worse, so I want to fix them ahead of time before dealing with FGDB SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 07:05:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369748#M8761</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-15T07:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369943#M8770</link>
      <description>&lt;P&gt;The screenshot with the desired outcome appears to be greatest-n-per-group with "n" being 1 where the greatest has ties.&amp;nbsp; Are you interested in greatest-singular or greatest-n?&amp;nbsp; If the former, it simplifies the situation a bit.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE&lt;/STRONG&gt;:&amp;nbsp; The following is a generalized greatest-n-per-group that supports ties in SQLite.&amp;nbsp; I can't speak to its performance, and it is a bit ugly with the nested subquery (nesting is forced with a window function), but it works and is a place to start:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;ObjectID IN (
  SELECT
    ObjectID
  FROM
    (
      SELECT
        ObjectID,
        DENSE_RANK () OVER (
          PARTITION BY t_species
          ORDER BY t_date DESC
        ) AS date_rank
      FROM
        speciesrecords
    ) 
  WHERE 
    date_rank &amp;lt;= 1
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The date_rank controls the "n" being selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jan 2024 23:16:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369943#M8770</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-13T23:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369946#M8771</link>
      <description>&lt;P&gt;Thanks! It's good to have solutions for both scenarios:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Has ties&lt;/LI&gt;&lt;LI&gt;Or, no ties&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I'll follow up on your question and #2 in a separate reply.&lt;/P&gt;&lt;P&gt;But first, just to clarify regarding #1, are there any issues with the SQL expression I provided in the original post (the expression I wrote, not the one from the article).&lt;/P&gt;&lt;PRE&gt;t_date in (
    select
        max(t_date)
    from
        species_records s2 
&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;    where
        s2.t_species = species_records.t_species)&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;I've always had a bit of a mental block when it comes to subqueries in the WHERE clause. When I look at my solution, it seems like it shouldn't work. But maybe it does?&lt;/P&gt;&lt;P&gt;In other words, is the resultset from my expression any different than the resultset from yours?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2024 00:24:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369946#M8771</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-14T00:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369956#M8772</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I'm really struggling with subqueries in these FGDB views.&amp;nbsp; They seems temperamental with warnings about 'cannot be edited in clause mode' when I'm not in clause mode&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;My understanding is that clause mode is the non-SQL mode. I think it's the mode where you construct a query using the UI pick lists.&lt;/P&gt;&lt;P&gt;If I'm writing a complex SQL query in SQL mode, then it makes sense to get the message, "The expression can't be edited in clause mode", since that complex SQL wouldn't be parsable into the UI pick lists. I just ignore that message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you seeing something different?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 00:00:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369956#M8772</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-15T00:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369958#M8773</link>
      <description>&lt;P&gt;Here's a mobile geodatabase technique for #2 - no ties.&lt;/P&gt;&lt;PRE&gt;species_records.objectid IN (
         SELECT objectid
           FROM species_records r2
          WHERE r2.t_species = species_records.t_species
       ORDER BY T_date DESC  --, priority ASC
          &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;LIMIT 1&lt;/STRONG&gt;&lt;/FONT&gt;
                )&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_1-1705204060948.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91528iB3E3F7489EB0021F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_1-1705204060948.png" alt="Bud_1-1705204060948.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And a similar technique that works for Oracle:&lt;/P&gt;&lt;PRE&gt;species_records.objectid IN (
         SELECT objectid
           FROM species_records s2
          WHERE s2.t_species = species_records.t_species
       ORDER BY t_date DESC --, priority ASC
          &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;FETCH FIRST ROW ONLY&lt;/STRONG&gt;&lt;/FONT&gt;
                )&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Jan 2024 03:52:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1369958#M8773</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-14T03:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370019#M8774</link>
      <description>&lt;P&gt;For n=1, the results of your query and mine are the same.&amp;nbsp; That said, there are differences.&amp;nbsp; As I already mentioned, your query is only applicable for n=1 so it would not work in other n-cases.&amp;nbsp; Your query relies on a correlated subquery whereas mine relies on a couple co-routine calls and a couple subquery scans.&amp;nbsp; The correlated subquery is an expensive operation, but having more execution steps like in mine can add up as well.&amp;nbsp; How the two approaches perform relative to data set size?&amp;nbsp; I don't know.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since SQLite supports a row value on the left-hand side of the IN operator, an alternative to n=1 that doesn't require a correlated subquery is:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;(t_species, t_date) IN (
  SELECT 
    t_species, 
    max(t_date) 
  FROM 
    species_records 
  GROUP BY 
    t_species
)&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 14 Jan 2024 15:50:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370019#M8774</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-14T15:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370036#M8775</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;Since SQLite supports a row value on the left-hand side of the IN operator, an alternative to n=1 that doesn't require a correlated subquery is:&lt;/P&gt;&lt;PRE&gt;(t_species, t_date) IN (
  SELECT 
    t_species, 
    max(t_date) 
  FROM 
    species_records 
  GROUP BY 
    t_species&lt;BR /&gt;)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Very interesting. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mobile Geodatabase/SQLite:&lt;/P&gt;&lt;P&gt;For my notes, I'm confirming that the approach works in a mobile geodatabase. Greatest 1 per group (with ties), as expected.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_0-1705266753825.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91563i6AADB97D2B153430/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_0-1705266753825.png" alt="Bud_0-1705266753825.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Oracle 18c:&lt;/P&gt;&lt;P&gt;I was pleasantly surprised to see it also works in Oracle 18c. I didn't think Oracle supported "a row value on the left-hand side of the IN operator". But it does.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_1-1705266960006.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91564iE4902249E6D74206/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_1-1705266960006.png" alt="Bud_1-1705266960006.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Oracle 18c demo:&amp;nbsp;&lt;A href="https://dbfiddle.uk/GznqkwlQ" target="_blank" rel="noopener"&gt;https://dbfiddle.uk/GznqkwlQ&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1371"&gt;@JoshuaBixby&lt;/a&gt;, you might find this video on Oracle's KEEP clause interesting.&amp;nbsp;&lt;A href="https://www.youtube.com/watch?v=AlTI_ZUyE0U&amp;amp;t=2s" target="_self"&gt;The KEEP clause will KEEP your SQL queries SIMPLE!&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;File Geodatabase:&lt;/P&gt;&lt;P&gt;Unsurprisingly, it doesn't work in file geodatabases.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_3-1705267190789.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91566iEDBF7055E21A987B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_3-1705267190789.png" alt="Bud_3-1705267190789.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;SQL Server:&lt;/P&gt;&lt;P&gt;I couldn't get it to work in SQL Server:&amp;nbsp;&lt;A href="https://dbfiddle.uk/dtiYKrf_" target="_blank" rel="noopener"&gt;https://dbfiddle.uk/dtiYKrf_&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Postgres:&lt;/P&gt;&lt;P&gt;From a quick googling, it looks like that syntax isn't supported in PostgreSQL either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 15:32:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370036#M8775</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-15T15:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370039#M8776</link>
      <description>&lt;P&gt;I'd like to ask a moderator to move this string of replies to our FGDB view post, since this current post is mostly about mobile geodatabases, not FGDBs. &lt;STRONG&gt;Is that ok with you?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Move to:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/selecting-the-most-recent-records-based-on-unique/m-p/1365493/highlight/true#M76860" target="_self"&gt;Selecting the most recent records based on unique values in another field&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2024 21:25:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370039#M8776</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-14T21:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370175#M8777</link>
      <description>&lt;P&gt;It is supported in Postgres, it is called a row constructor on the left-hand side instead of row value, but the same syntax works for both:&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.postgresql.org/docs/current/functions-subquery.html#FUNCTIONS-SUBQUERY-IN" target="_blank" rel="noopener"&gt;https://www.postgresql.org/docs/current/functions-subquery.html#FUNCTIONS-SUBQUERY-IN&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;PRE&gt;&lt;EM&gt;row_constructor&lt;/EM&gt; IN (&lt;EM&gt;subquery&lt;/EM&gt;)&lt;/PRE&gt;&lt;P&gt;The left-hand side of this form of IN is a row constructor, as described in &lt;A title="4.2.13.&amp;nbsp;Row Constructors" href="https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS" target="_blank" rel="noopener"&gt;Section&amp;nbsp;4.2.13&lt;/A&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 15:17:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370175#M8777</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-15T15:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370328#M8783</link>
      <description>&lt;P&gt;Do you figure it's logically impossible to get the greatest 1 per group (ties or no ties) in a FGDB SQL expression using current FGDB SQL functionality?&lt;/P&gt;&lt;P&gt;Since none of the following are supported:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Correlated subqueries&lt;/LI&gt;&lt;LI&gt;A row value on the left-hand side of the IN operator&lt;/LI&gt;&lt;LI&gt;Concatenating a string and a date into a poor man's multi-field join:&lt;BR /&gt;&lt;PRE&gt;--works in normal databases, but not file geodatabases, due to limited concatenation functionality&lt;BR /&gt;t_species||' '||t_date in (
    select
        t_species||' '||max(t_date)
    from
        species_records
    group by
        t_species
)      &lt;/PRE&gt;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Tue, 16 Jan 2024 17:36:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370328#M8783</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-16T17:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370587#M8785</link>
      <description>&lt;P&gt;I wouldn't say it is logically impossible but technically impossible because of Esri's SQL implementation.&lt;/P&gt;&lt;P&gt;Concatenating text with a date is possible, in certain places, with file geodatabases, but the date must be CAST to text before concatenating:&amp;nbsp; &lt;A href="https://pro.arcgis.com/en/pro-app/latest/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm" target="_blank" rel="noopener"&gt;SQL reference for query expressions used in ArcGIS—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;H2&gt;CAST function&lt;/H2&gt;&lt;P&gt;The&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;CAST()&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function converts a value or an expression from one data type to another specified data type. The syntax is as follows:&lt;/P&gt;&lt;P&gt;CAST&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;(expression&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;AS&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;data_type(length))&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;When casting a date to text in a file geodatabase, the outputted date format is:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;File geodatabases support the use of a time in the date field, so this can be added to the expression:&lt;/P&gt;&lt;PRE&gt;Datefield = timestamp 'yyyy-mm-dd hh:mm:ss'&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;So,&amp;nbsp; 2/2/2021 3:00 PM casted to text is '2021-02-02 15:00:00'.&lt;/P&gt;&lt;P&gt;Although dates can be cast to text and concatenated with additional text, e.g., a species name, it apparently isn't allowed within a subquery, so that takes it off the table in this case.&amp;nbsp; The following SQL works as a file geodatabase view&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;SELECT 
  MAX(t_species || CAST(t_date AS CHAR(20))) 
FROM 
  species_records 
GROUP BY 
  t_species&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but embedding that same SQL into a subquery (even a subquery in another file geodatabase view) generates an error.&amp;nbsp; &lt;EM&gt;Note:&amp;nbsp; The MAX function needs to be applied to the concatenated result and not just the date field.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 17:22:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370587#M8785</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-16T17:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370700#M8786</link>
      <description>&lt;P&gt;Interesting.&lt;/P&gt;&lt;P&gt;I couldn't even get CAST() to work on its own in the WHERE clause in a simple view.&lt;/P&gt;&lt;PRE&gt;select
    *
from
    species_records
where
    cast(t_date as char(50)) is not null &lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_1-1705436912531.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91751i766D368D721E9DF3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_1-1705436912531.png" alt="Bud_1-1705436912531.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;ERROR 160195: An invalid SQL statement was used.&lt;/PRE&gt;&lt;P&gt;But that query works fine in Oracle and in a mobile geodatabase.&lt;/P&gt;&lt;P&gt;I'll report it to support as a FGDB bug.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Side note:&lt;/P&gt;&lt;P&gt;I left some feedback recently on the FGDB SQL page:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/manage-file-gdb/sql-reporting-and-anlysis-file-geodatabases.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/manage-file-gdb/sql-reporting-and-anlysis-file-geodatabases.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Esri wrote back today and said, &lt;EM&gt;"The page you reference has been mostly rewritten for the upcoming release, so you will see many changes in it later this year..."&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Fingers crossed those docs have been improved.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 21:14:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370700#M8786</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-16T21:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370716#M8787</link>
      <description>&lt;P&gt;Rewriting the documentation and rewriting the SQL support are very different, and I suspect they only mean the former and not the latter.&amp;nbsp; That said, the documentation can definitely be polished up.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 21:15:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370716#M8787</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-16T21:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370723#M8788</link>
      <description>&lt;P&gt;Regarding not being able to create a database view with CAST in the WHERE clause, that is interesting because CAST works in the Select By Attributes tool, which of course is the WHERE clause:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;CAST(t_date AS CHAR(20)) IS NOT NULL&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 21:29:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1370723#M8788</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-16T21:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1372187#M8812</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;SPAN class=""&gt;Esri Case 03529800 - FGDB database view: Can't use CAST function in WHERE clause&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;SPAN class=""&gt;&amp;nbsp;&lt;BR /&gt;BUG-000164429: When creating a database view, using a cast function in the where clause results in an error&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 19 Jan 2024 12:08:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1372187#M8812</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-19T12:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum values in Select By Attributes (greatest n per group)</title>
      <link>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1372376#M8813</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;Regarding not being able to create a database view with CAST in the WHERE clause, that is interesting because CAST works in the Select By Attributes tool, which of course is the WHERE clause:&lt;/P&gt;&lt;P&gt;CAST(t_date AS CHAR(20)) IS NOT NULL&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I looked at the Diagnostic Monitor in ArcGIS Pro to see what SQL query Pro sends to the database when using CAST in Select By Attributes. I had wondered if it was doing something special, such as wrapping something in a subquery or outer query, which can be an effective workaround in some cases.&lt;/P&gt;&lt;P&gt;Examples:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/create-fgdb-database-view-using-the-order-by/m-p/1371894#M77617" target="_self"&gt;Create FGDB database view using the ORDER BY clause: ERROR 999999&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;There is an Esri bug where ST_GEOMETRY functions in Oracle can’t be used in a SQL Expression (definition query, attribute table, etc.) when the FC is versioned. Wrapping the WHERE clause in a subquery lets us work around that bug.&lt;UL&gt;&lt;LI&gt;Case #03080875 - ST_GEOMETRY functions used in Select By Attributes tool on Shape field generate error "ORA-00904 Invalid Identifier "SHAPE"&lt;/LI&gt;&lt;LI&gt;BUG-000150273&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/find-multi-part-features-using-sql-st-geometry/m-p/1186467/highlight/true#M56563" target="_blank" rel="noopener"&gt;https://community.esri.com/t5/arcgis-pro-questions/find-multi-part-features-using-sql-st-geometry/m-...&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;BR /&gt;But no, the query looks normal.&lt;/P&gt;&lt;PRE&gt;SELECT OBJECTID FROM species_records WHERE CAST(t_date AS CHAR(20)) IS NOT NULL&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_0-1705685841853.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92234i3577F8717CB67E68/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_0-1705685841853.png" alt="Bud_0-1705685841853.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I tried using that exact SQL to create a FGDB database view. But it failed, just like previous attempts to use CAST in the WHERE clause of a FGDB database view.&lt;/P&gt;&lt;P&gt;I also tried adding an extra column to the SELECT clause, just in case it didn't like only selecting the ObjectID. But that didn't work either.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;SELECT OBJECTID, &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;t_species&lt;/STRONG&gt;&lt;/FONT&gt; FROM species_records WHERE CAST(t_date AS CHAR(20)) IS NOT NULL&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN&gt;ERROR 160195: An invalid SQL statement was used.&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;Of course, I don't have this problem in mobile or enterprise GDB database views.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jan 2024 08:50:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/select-maximum-values-in-select-by-attributes/m-p/1372376#M8813</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-21T08:50:49Z</dc:date>
    </item>
  </channel>
</rss>

