<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Eddie Awad’s Blog - Latest Comments in Keep first, Keep last</title><link>http://awads.disqus.com/</link><description>News, views, tips and tricks on Oracle and other fun stuff</description><language>en</language><lastBuildDate>Thu, 02 Feb 2006 15:25:21 -0000</lastBuildDate><item><title>Re: Keep first, Keep last</title><link>http://awads.net/wp/2006/02/02/keep-first-keep-last/#comment-3658311</link><description>&lt;p&gt;Anthony, you're right. That's an important distinction. So, to test, I added the following two rows to t:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;insert into t (n) values (400)&lt;br&gt;/&lt;br&gt;insert into t (n) values (400)&lt;br&gt;/&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;Then, I ran the first query:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;HR@XE&amp;gt; select n&lt;br&gt;  2  from&lt;br&gt;  3    (select n,&lt;br&gt;  4       dense_rank() over(&lt;br&gt;  5     order by cnt desc) as&lt;br&gt;  6    rnk&lt;br&gt;  7     from&lt;br&gt;  8      (select n,&lt;br&gt;  9         count(*) as&lt;br&gt; 10     cnt&lt;br&gt; 11      from t&lt;br&gt; 12      group by n)&lt;br&gt; 13   x)&lt;br&gt; 14  y&lt;br&gt; 15  where rnk = 1&lt;br&gt; 16  /&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;I got:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;         N&lt;br&gt;----------&lt;br&gt;       400&lt;br&gt;       300&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;When I ran the second query:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;HR@XE&amp;gt; select max(n) keep(dense_rank first&lt;br&gt;  2  order by cnt desc) n&lt;br&gt;  3  from&lt;br&gt;  4    (select n,&lt;br&gt;  5       count(*) cnt&lt;br&gt;  6     from t&lt;br&gt;  7     group by n)&lt;br&gt;  8  /&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;I got:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;         N&lt;br&gt;----------&lt;br&gt;       400&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;Which proves that KEEP...FIRST will only keep one value: the greatest number.&lt;/p&gt;&lt;br&gt;&lt;p&gt;Of course, you could change to first query to select the maximum:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;select max(n) from ( ...first query ...)&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;which effectively makes it return the same result as the one with KEEP in this example.&lt;/p&gt;&lt;br&gt;&lt;p&gt;However, testing stats_mode:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;HR@XE&amp;gt; select stats_mode(n) from t;&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;Returned:&lt;/p&gt;&lt;br&gt;&amp;lt;pre&amp;gt;&lt;br&gt;STATS_MODE(N)&lt;br&gt;-------------&lt;br&gt;          300&lt;br&gt;&amp;lt;/pre&amp;gt;&lt;br&gt;&lt;p&gt;stats_mode does not work in 9i or below.&lt;/p&gt;&lt;br&gt;&lt;p&gt;Thanks for this clarification.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">eddieawad</dc:creator><pubDate>Thu, 02 Feb 2006 15:25:21 -0000</pubDate></item><item><title>Re: Keep first, Keep last</title><link>http://awads.net/wp/2006/02/02/keep-first-keep-last/#comment-3658310</link><description>&lt;p&gt;The two example queries will not return the same results in general.&lt;/p&gt;&lt;br&gt;&lt;p&gt;If there are 2 values of n in the table with equal maximum frequencies (i.e. two modes), then the first query will return both top ranked values.  The second query will return the &lt;i&gt;maximum&lt;/i&gt; of the two top-ranked values.&lt;/p&gt;&lt;br&gt;&lt;p&gt;Also, see:&lt;/p&gt;&lt;br&gt;&lt;p&gt;select stats_mode(n) from t;&lt;/p&gt;&lt;br&gt;&lt;p&gt;... which will return strictly &lt;i&gt;one&lt;/i&gt; of the modes, although we can't predict which one.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anthony Wilson</dc:creator><pubDate>Thu, 02 Feb 2006 10:51:44 -0000</pubDate></item></channel></rss>