<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Keep first, Keep last</title>
	<atom:link href="http://awads.net/wp/2006/02/02/keep-first-keep-last/feed/" rel="self" type="application/rss+xml" />
	<link>http://awads.net/wp/2006/02/02/keep-first-keep-last/</link>
	<description>News, views, tips and tricks on Oracle and other fun stuff</description>
	<lastBuildDate>Wed, 17 Mar 2010 02:29:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Eddie Awad</title>
		<link>http://awads.net/wp/2006/02/02/keep-first-keep-last/comment-page-1/#comment-1311</link>
		<dc:creator>Eddie Awad</dc:creator>
		<pubDate>Thu, 02 Feb 2006 20:25:21 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=207#comment-1311</guid>
		<description>&lt;p&gt;Anthony, you&#039;re right. That&#039;s an important distinction. So, to test, I added the following two rows to t:&lt;/p&gt;

&lt;pre&gt;
insert into t (n) values (400)
/
insert into t (n) values (400)
/
&lt;/pre&gt;

&lt;p&gt;Then, I ran the first query:&lt;/p&gt;

&lt;pre&gt;
HR@XE&gt; select n
  2  from
  3    (select n,
  4       dense_rank() over(
  5     order by cnt desc) as
  6    rnk
  7     from
  8      (select n,
  9         count(*) as
 10     cnt
 11      from t
 12      group by n)
 13   x)
 14  y
 15  where rnk = 1
 16  /
&lt;/pre&gt;

&lt;p&gt;I got:&lt;/p&gt;

&lt;pre&gt;
         N
----------
       400
       300
&lt;/pre&gt;

&lt;p&gt;When I ran the second query:&lt;/p&gt;

&lt;pre&gt;
HR@XE&gt; select max(n) keep(dense_rank first
  2  order by cnt desc) n
  3  from
  4    (select n,
  5       count(*) cnt
  6     from t
  7     group by n)
  8  /
&lt;/pre&gt;

&lt;p&gt;I got:&lt;/p&gt;

&lt;pre&gt;
         N
----------
       400
&lt;/pre&gt;

&lt;p&gt;Which proves that KEEP...FIRST will only keep one value: the greatest number.&lt;/p&gt;

&lt;p&gt;Of course, you could change to first query to select the maximum:&lt;/p&gt;

&lt;pre&gt;
select max(n) from ( ...first query ...)
&lt;/pre&gt;

&lt;p&gt;which effectively makes it return the same result as the one with KEEP in this example.&lt;/p&gt;

&lt;p&gt;However, testing stats_mode:&lt;/p&gt;

&lt;pre&gt;
HR@XE&gt; select stats_mode(n) from t;
&lt;/pre&gt;

&lt;p&gt;Returned:&lt;/p&gt;

&lt;pre&gt;
STATS_MODE(N)
-------------
          300
&lt;/pre&gt;

&lt;p&gt;stats_mode does not work in 9i or below.&lt;/p&gt;

&lt;p&gt;Thanks for this clarification.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Anthony, you&#8217;re right. That&#8217;s an important distinction. So, to test, I added the following two rows to t:</p>

<pre>
insert into t (n) values (400)
/
insert into t (n) values (400)
/
</pre>

<p>Then, I ran the first query:</p>

<pre>
HR@XE&gt; select n
  2  from
  3    (select n,
  4       dense_rank() over(
  5     order by cnt desc) as
  6    rnk
  7     from
  8      (select n,
  9         count(*) as
 10     cnt
 11      from t
 12      group by n)
 13   x)
 14  y
 15  where rnk = 1
 16  /
</pre>

<p>I got:</p>

<pre>
         N
----------
       400
       300
</pre>

<p>When I ran the second query:</p>

<pre>
HR@XE&gt; select max(n) keep(dense_rank first
  2  order by cnt desc) n
  3  from
  4    (select n,
  5       count(*) cnt
  6     from t
  7     group by n)
  8  /
</pre>

<p>I got:</p>

<pre>
         N
----------
       400
</pre>

<p>Which proves that KEEP&#8230;FIRST will only keep one value: the greatest number.</p>

<p>Of course, you could change to first query to select the maximum:</p>

<pre>
select max(n) from ( ...first query ...)
</pre>

<p>which effectively makes it return the same result as the one with KEEP in this example.</p>

<p>However, testing stats_mode:</p>

<pre>
HR@XE&gt; select stats_mode(n) from t;
</pre>

<p>Returned:</p>

<pre>
STATS_MODE(N)
-------------
          300
</pre>

<p>stats_mode does not work in 9i or below.</p>

<p>Thanks for this clarification.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Wilson</title>
		<link>http://awads.net/wp/2006/02/02/keep-first-keep-last/comment-page-1/#comment-1310</link>
		<dc:creator>Anthony Wilson</dc:creator>
		<pubDate>Thu, 02 Feb 2006 15:51:44 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=207#comment-1310</guid>
		<description>&lt;p&gt;The two example queries will not return the same results in general.&lt;/p&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;p&gt;Also, see:&lt;/p&gt;

&lt;p&gt;select stats_mode(n) from t;&lt;/p&gt;

&lt;p&gt;... which will return strictly &lt;i&gt;one&lt;/i&gt; of the modes, although we can&#039;t predict which one.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The two example queries will not return the same results in general.</p>

<p>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 <i>maximum</i> of the two top-ranked values.</p>

<p>Also, see:</p>

<p>select stats_mode(n) from t;</p>

<p>&#8230; which will return strictly <i>one</i> of the modes, although we can&#8217;t predict which one.</p>]]></content:encoded>
	</item>
</channel>
</rss>
