<?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/"
		>
<channel>
	<title>Comments on: How to select the first/least/max row per group in SQL</title>
	<atom:link href="http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/</link>
	<description>Stay curious!</description>
	<lastBuildDate>Thu, 09 Feb 2012 20:41:20 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Stephen</title>
		<link>http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/#comment-19854</link>
		<dc:creator>Stephen</dc:creator>
		<pubDate>Sun, 29 Jan 2012 22:07:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=145#comment-19854</guid>
		<description>Someone else pointed to a cursor solution. I found this considerable faster than the other solutions and far easier to understand. On my non too impressive box 12,000 rows are processed in 0.00.35.

This gets up to the 10 highest graded locations - same problem as the fruit. Setting temp table engine to memory makes it slightly faster still.

CREATE PROCEDURE getBestByCountry  ()
BEGIN

  DECLARE done INT DEFAULT FALSE;
  DECLARE _country  VARCHAR(40);
  DECLARE cur1 CURSOR FOR SELECT DISTINCT country FROM Location;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur1;

  DROP TABLE IF EXISTS best;

  CREATE temporary TABLE best 
  ( id  INT UNSIGNED )   ;

  read_loop: LOOP
    FETCH cur1 INTO _country;
    IF done THEN
      LEAVE read_loop;
    END IF;

    INSERT INTO best (id)
    SELECT id FROM Location WHERE country = _country
    ORDER BY grade DESC
    LIMIT 10;

  END LOOP;

  CLOSE cur1;

 SELECT b.*  -- get rest of data
 FROM best t,
      listBest b
 WHERE t.id = b.id
 ORDER BY b.country, b.grade DESC, b.overallRating DESC
    ;

END;</description>
		<content:encoded><![CDATA[<p>Someone else pointed to a cursor solution. I found this considerable faster than the other solutions and far easier to understand. On my non too impressive box 12,000 rows are processed in 0.00.35.</p>
<p>This gets up to the 10 highest graded locations &#8211; same problem as the fruit. Setting temp table engine to memory makes it slightly faster still.</p>
<p>CREATE PROCEDURE getBestByCountry  ()<br />
BEGIN</p>
<p>  DECLARE done INT DEFAULT FALSE;<br />
  DECLARE _country  VARCHAR(40);<br />
  DECLARE cur1 CURSOR FOR SELECT DISTINCT country FROM Location;<br />
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;</p>
<p>  OPEN cur1;</p>
<p>  DROP TABLE IF EXISTS best;</p>
<p>  CREATE temporary TABLE best<br />
  ( id  INT UNSIGNED )   ;</p>
<p>  read_loop: LOOP<br />
    FETCH cur1 INTO _country;<br />
    IF done THEN<br />
      LEAVE read_loop;<br />
    END IF;</p>
<p>    INSERT INTO best (id)<br />
    SELECT id FROM Location WHERE country = _country<br />
    ORDER BY grade DESC<br />
    LIMIT 10;</p>
<p>  END LOOP;</p>
<p>  CLOSE cur1;</p>
<p> SELECT b.*  &#8212; get rest of data<br />
 FROM best t,<br />
      listBest b<br />
 WHERE t.id = b.id<br />
 ORDER BY b.country, b.grade DESC, b.overallRating DESC<br />
    ;</p>
<p>END;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ludovic</title>
		<link>http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/#comment-19850</link>
		<dc:creator>Ludovic</dc:creator>
		<pubDate>Tue, 24 Jan 2012 18:49:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=145#comment-19850</guid>
		<description>Thank you so much. Now my app looks serious :-))</description>
		<content:encoded><![CDATA[<p>Thank you so much. Now my app looks serious :-))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martha</title>
		<link>http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/#comment-19823</link>
		<dc:creator>Martha</dc:creator>
		<pubDate>Tue, 10 Jan 2012 02:55:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=145#comment-19823</guid>
		<description>Super helpful. Thanks for posting!</description>
		<content:encoded><![CDATA[<p>Super helpful. Thanks for posting!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Yousef</title>
		<link>http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/#comment-19813</link>
		<dc:creator>Jason Yousef</dc:creator>
		<pubDate>Fri, 06 Jan 2012 18:18:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=145#comment-19813</guid>
		<description>Excellent! Thank you for this info.</description>
		<content:encoded><![CDATA[<p>Excellent! Thank you for this info.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean</title>
		<link>http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/#comment-19788</link>
		<dc:creator>Sean</dc:creator>
		<pubDate>Thu, 15 Dec 2011 19:58:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=145#comment-19788</guid>
		<description>Thanks for this post, I was able to use the ideas to get the most recent and then everything that is not the most recent. Made life a lot easier, at least for this problem, onto the next.</description>
		<content:encoded><![CDATA[<p>Thanks for this post, I was able to use the ideas to get the most recent and then everything that is not the most recent. Made life a lot easier, at least for this problem, onto the next.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

