<?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 or last row per group in SQL</title>
	<atom:link href="http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-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: Webmaster</title>
		<link>http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-18152</link>
		<dc:creator>Webmaster</dc:creator>
		<pubDate>Sat, 17 Apr 2010 07:40:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-18152</guid>
		<description>Here is a great simple and efficient solution to the problem. It&#039;s close to just saying FIRST() LAST() in MySQL

http://topwebguy.com/first-and-last-in-mysql-a-working-solution/</description>
		<content:encoded><![CDATA[<p>Here is a great simple and efficient solution to the problem. It&#8217;s close to just saying FIRST() LAST() in MySQL</p>
<p><a href="http://topwebguy.com/first-and-last-in-mysql-a-working-solution/" rel="nofollow">http://topwebguy.com/first-and-last-in-mysql-a-working-solution/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simulating First/Last aggregate functions in MySQL &#124; zonalivre.org</title>
		<link>http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17100</link>
		<dc:creator>Simulating First/Last aggregate functions in MySQL &#124; zonalivre.org</dc:creator>
		<pubDate>Mon, 12 Oct 2009 00:17:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17100</guid>
		<description>[...] came across an interesting post at Xaprb.com on selecting the first and last row of a group in MySQL.  This solution builds on a comment in [...]</description>
		<content:encoded><![CDATA[<p>[...] came across an interesting post at Xaprb.com on selecting the first and last row of a group in MySQL.  This solution builds on a comment in [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scot</title>
		<link>http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17038</link>
		<dc:creator>Scot</dc:creator>
		<pubDate>Mon, 28 Sep 2009 21:19:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17038</guid>
		<description>I ran into a scenario which required me to use the last row of a group, and after a lot of searching and looking up all the usual methods I came up with one which I thought I&#039;d share incase anyone else does.

I did not use any sub-queries or the likes due to how slow they are, but instead made use of the GROUP_CONCAT() function.

In my scenario, I had a table which had rows of data for every &quot;minute&quot;, each row contained 4 main values I needed: open, close, high, low. I made my query to group this data into 5 minute intervals, such as: 5:00PM, 5:05PM, 5:10PM and so on. The high/low was easily accomplished by MIN/MAX, the open was correct because &quot;group by&quot; returns the first rows &quot;open&quot; value, but obviously &quot;close&quot; was incorrect and I needed the last row of the group. The main method mentioned here did not do me any good due to the large amount of data I was working with, and my solution was simply this (this is not the exact query):

----------------------------
select
open, MAX(high), MIN(low), SUBSTRING_INDEX(GROUP_CONCAT(CAST(close as CHAR)), &#039;,&#039;, -1)

from mytable

group by [xyz...]
----------------------------

The group_concat() will return all &quot;close&quot; values within the group seperated by a comma, I used CAST() because the values are FLOAT and would otherwise result in BLOB, lastly the SUBSTRING_INDEX() function was used to retrieve the last item in the string after the final comma.

Works like a charm!</description>
		<content:encoded><![CDATA[<p>I ran into a scenario which required me to use the last row of a group, and after a lot of searching and looking up all the usual methods I came up with one which I thought I&#8217;d share incase anyone else does.</p>
<p>I did not use any sub-queries or the likes due to how slow they are, but instead made use of the GROUP_CONCAT() function.</p>
<p>In my scenario, I had a table which had rows of data for every &#8220;minute&#8221;, each row contained 4 main values I needed: open, close, high, low. I made my query to group this data into 5 minute intervals, such as: 5:00PM, 5:05PM, 5:10PM and so on. The high/low was easily accomplished by MIN/MAX, the open was correct because &#8220;group by&#8221; returns the first rows &#8220;open&#8221; value, but obviously &#8220;close&#8221; was incorrect and I needed the last row of the group. The main method mentioned here did not do me any good due to the large amount of data I was working with, and my solution was simply this (this is not the exact query):</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
select<br />
open, MAX(high), MIN(low), SUBSTRING_INDEX(GROUP_CONCAT(CAST(close as CHAR)), &#8216;,&#8217;, -1)</p>
<p>from mytable</p>
<p>group by [xyz...]<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>The group_concat() will return all &#8220;close&#8221; values within the group seperated by a comma, I used CAST() because the values are FLOAT and would otherwise result in BLOB, lastly the SUBSTRING_INDEX() function was used to retrieve the last item in the string after the final comma.</p>
<p>Works like a charm!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zaman</title>
		<link>http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-14228</link>
		<dc:creator>Zaman</dc:creator>
		<pubDate>Thu, 21 Feb 2008 05:29:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-14228</guid>
		<description>This is not helping specially when you are shortening  the queries , you are writing in detail but you come to practice you are shorting your only way to explain what are you talking about..</description>
		<content:encoded><![CDATA[<p>This is not helping specially when you are shortening  the queries , you are writing in detail but you come to practice you are shorting your only way to explain what are you talking about..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artem Nosulchik</title>
		<link>http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-13374</link>
		<dc:creator>Artem Nosulchik</dc:creator>
		<pubDate>Wed, 05 Sep 2007 07:38:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-13374</guid>
		<description>Thank you. Nice tips!</description>
		<content:encoded><![CDATA[<p>Thank you. Nice tips!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

