<?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: Simplify Oracle aggregation queries (using KEEP, DENSE_RANK, &amp;amp; FIRST/LAST) in ArcGIS Enterprise Questions</title>
    <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1266796#M35372</link>
    <description>&lt;P&gt;Thanks a lot &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/351335"&gt;@Bud&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate all your help very much. I'm glad that SQLite has some support for this. I hope they will grow that support and other databases will follow the suite.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 11 Mar 2023 01:34:05 GMT</pubDate>
    <dc:creator>TanuHoque</dc:creator>
    <dc:date>2023-03-11T01:34:05Z</dc:date>
    <item>
      <title>Simplify Oracle aggregation queries (using the KEEP clause)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264042#M35288</link>
      <description>&lt;P&gt;I thought I'd share an SQL technique I came across recently — for anyone writing aggregation queries for Oracle:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;YouTube video:&amp;nbsp;&lt;A href="https://www.youtube.com/watch?v=AlTI_ZUyE0U&amp;amp;t=20s" target="_self"&gt;The KEEP clause will KEEP your SQL queries SIMPLE&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Paraphrased:&lt;BR /&gt;When doing aggregation, one of the most common requirements is: once we've aggregated on a particular column, we actually want to know information from a different column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;BR /&gt;For a given country, what city has the highest population? (where the country has more than one city)&lt;BR /&gt;Include the city name as a column.&lt;/P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_1-1677860737930.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/64296i9C4B9E8024EF9D1E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bud_1-1677860737930.png" alt="Bud_1-1677860737930.png" /&gt;&lt;/span&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I recommend watching that video.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Then, for an explanation of the logic of the various SQL keywords, I'd suggest reading the answer in this post:&lt;BR /&gt;Stack Overflow -&amp;nbsp;&lt;A href="https://stackoverflow.com/a/65868727/5576771" target="_self"&gt;Explanation of KEEP in Oracle FIRST/LAST&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;One of the biggest challenges I have when writing SQL queries is keeping them short enough so that they're readable. I think the above technique will help.&lt;/P&gt;&lt;P&gt;While this technique doesn't pertain specifically to spatial data, I often write non-spatial queries like the above query &lt;U&gt;&lt;EM&gt;on&lt;/EM&gt;&lt;/U&gt; spatial data. I imagine others do too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Related:&lt;/P&gt;&lt;P&gt;Oracle Groundbreakers Community -&amp;nbsp;&lt;A href="https://forums.oracle.com/ords/apexds/post/idea-first-and-last-aggregate-functions-1055" target="_self"&gt;Idea: FIRST() and LAST() aggregate functions&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 15:34:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264042#M35288</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2023-03-14T15:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify Oracle aggregation queries (using KEEP and FIRST/LAST)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264080#M35293</link>
      <description>&lt;P&gt;Thanks for sharing this &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/351335"&gt;@Bud&lt;/a&gt;. This so cool. &lt;span class="lia-unicode-emoji" title=":1st_place_medal:"&gt;🥇&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Do you happen to know if this only specific to Oracle? Do other databases support this or something similar?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2023 17:32:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264080#M35293</guid>
      <dc:creator>TanuHoque</dc:creator>
      <dc:date>2023-03-03T17:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify Oracle aggregation queries (using KEEP and FIRST/LAST)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264145#M35297</link>
      <description>&lt;P&gt;SQL Server:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Stack Overflow: &lt;A href="https://stackoverflow.com/questions/75631146/sql-server-equivalent-to-oracles-max-keep-dense-rank-first-last-order-by" target="_self"&gt;SQL Server equivalent to Oracle's MAX(...) KEEP (DENSE_RANK FIRST/LAST ORDER BY ...)&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;Answer:&lt;/P&gt;&lt;P&gt;SQL Server does not implement this.&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;Edit:&lt;/STRONG&gt;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2620"&gt;@TanuHoque&lt;/a&gt;, I submitted an idea asking Microsoft to add that functionality to T-SQL:&lt;BR /&gt;Idea - &lt;A href="https://feedback.azure.com/d365community/idea/c29c9241-67bc-ed11-a81b-6045bd79fc6e" target="_self"&gt;SQL Server equivalent to Oracle's MAX(...) KEEP (DENSE_RANK FIRST/LAST ORDER BY ...)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Feel free to vote for that idea.&lt;/P&gt;&lt;P&gt;Related: &lt;A href="https://feedback.azure.com/d365community/forum/04fe6ee0-3b25-ec11-b6e6-000d3a4f0da0" target="_self"&gt;SQL Server Ideas&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Mar 2023 22:20:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264145#M35297</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2023-03-06T22:20:37Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify Oracle aggregation queries (using KEEP and FIRST/LAST)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264502#M35312</link>
      <description>&lt;P&gt;PostgreSQL:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DBA Stack Exchange: &lt;A href="https://dba.stackexchange.com/a/324429/100880" target="_self"&gt;PostgreSQL equivalent to Oracle's MAX(...) KEEP (DENSE_RANK FIRST/LAST ORDER BY ...)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;Edit:&lt;/STRONG&gt;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2620"&gt;@TanuHoque&lt;/a&gt;, I submitted a post to the "General" PostgreSQL mailing list -- requesting that the functionality be added to PostgreSQL. The post/email was called &lt;EM&gt;"Idea: PostgreSQL equivalent to Oracle's KEEP clause"&lt;/EM&gt;. The content of the request was the same as the SQL Server one I mentioned previously, except it was for PostgreSQL.&lt;/P&gt;&lt;P&gt;Related:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;DBA Stack Exchange - &lt;A href="https://dba.stackexchange.com/questions/324446/how-to-request-an-enhancement-to-postgresql" target="_self"&gt;How to request an enhancement to PostgreSQL&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://www.postgresql.org/list/" target="_self"&gt;PostgreSQL Mailing Lists&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Mon, 06 Mar 2023 22:21:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1264502#M35312</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2023-03-06T22:21:25Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify Oracle aggregation queries (using KEEP and FIRST/LAST)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1266031#M35360</link>
      <description>&lt;P&gt;SQLite&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2620"&gt;@TanuHoque&lt;/a&gt;&amp;nbsp;You'll find this SQLite answer interesting:&amp;nbsp;&lt;A href="https://stackoverflow.com/a/75687099/5576771" target="_self"&gt;SQLite equivalent to Oracle's MAX(...) KEEP (DENSE_RANK FIRST/LAST ORDER BY ...)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;See: &lt;A href="https://www.sqlite.org/lang_select.html#bare_columns_in_an_aggregate_query" target="_self"&gt;2.5. Bare columns in an aggregate query&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2023 16:20:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1266031#M35360</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2023-03-09T16:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify Oracle aggregation queries (using KEEP, DENSE_RANK, &amp; FIRST/LAST)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1266796#M35372</link>
      <description>&lt;P&gt;Thanks a lot &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/351335"&gt;@Bud&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate all your help very much. I'm glad that SQLite has some support for this. I hope they will grow that support and other databases will follow the suite.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Mar 2023 01:34:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1266796#M35372</guid>
      <dc:creator>TanuHoque</dc:creator>
      <dc:date>2023-03-11T01:34:05Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify Oracle aggregation queries (using the KEEP clause)</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1267595#M35382</link>
      <description>&lt;P&gt;For what it's worth, I've been using Oracle's new&amp;nbsp;&lt;FONT face="courier new,courier" color="#0000FF"&gt;ANY_VALUE()&lt;/FONT&gt; aggregate function instead of the &lt;FONT face="courier new,courier" color="#0000FF"&gt;MAX()&lt;/FONT&gt; aggregate function:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" color="#0000FF"&gt;&lt;STRONG&gt;ANY_VALUE&lt;/STRONG&gt;(CITY) KEEP (DENSE_RANK FIRST ORDER BY POPULATION DESC)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Reason (from a colleague):&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Knowing that you have &lt;FONT face="courier new,courier" color="#0000FF"&gt;max(city)&lt;/FONT&gt; there but that it will actually ignore that and get the city for the one with the highest population because of the &lt;/SPAN&gt;&lt;FONT face="courier new,courier" color="#0000FF"&gt;KEEP&lt;/FONT&gt;&lt;SPAN&gt; seems &lt;STRONG&gt;counterintuitive&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;While &lt;FONT face="courier new,courier" color="#0000FF"&gt;ANY_VALUE()&lt;/FONT&gt; will be "ignored" as well, at least it is more self-documenting. It suggests that the real logic&amp;nbsp; is in the other keywords:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="courier new,courier" color="#0000FF"&gt;... KEEP (DENSE_RANK FIRST &lt;STRONG&gt;ORDER BY POPULATION DESC&lt;/STRONG&gt;)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Related comments about &lt;FONT face="courier new,courier" color="#0000FF"&gt;ANY_VALUE()&lt;/FONT&gt;:&amp;nbsp;&lt;A href="https://stackoverflow.com/questions/65866812/explanation-of-keep-in-oracle-first-last/65868727?noredirect=1#comment133433061_65868727" target="_blank" rel="noopener"&gt;https://stackoverflow.com/questions/65866812/explanation-of-keep-in-oracle-first-last/65868727?noredirect=1#comment133433061_65868727&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Comments in the original YouTube video:&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bud_1-1678807921851.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/65216i7A4EFB4B9675DCCF/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bud_1-1678807921851.png" alt="Bud_1-1678807921851.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;db-oriented.com: &lt;A href="https://db-oriented.com/2021/02/28/any_value-and-first-last-keep/" target="_self"&gt;ANY_VALUE and FIRST/LAST (KEEP)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 23:44:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/simplify-oracle-aggregation-queries-using-the-keep/m-p/1267595#M35382</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2023-03-19T23:44:09Z</dc:date>
    </item>
  </channel>
</rss>

