Comments on: How to select the first or last row per group in SQL http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Ian Bugeja http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-20152 Ian Bugeja Mon, 13 Aug 2012 17:44:16 +0000 http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-20152 There is a simpler solution

You would just do an inner select and sort by the order you want in descending/ascending order

SELECT ip, id, word
FROM
( SELECT * FROM tb_a ORDER BY id DESC )
GROUP BY ip

]]>
By: Webmaster http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-18152 Webmaster Sat, 17 Apr 2010 07:40:34 +0000 http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-18152 Here is a great simple and efficient solution to the problem. It’s close to just saying FIRST() LAST() in MySQL

http://topwebguy.com/first-and-last-in-mysql-a-working-solution/

]]>
By: Simulating First/Last aggregate functions in MySQL | zonalivre.org http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17100 Simulating First/Last aggregate functions in MySQL | zonalivre.org Mon, 12 Oct 2009 00:17:57 +0000 http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17100 [...] 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 [...]

]]>
By: Scot http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17038 Scot Mon, 28 Sep 2009 21:19:02 +0000 http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-17038 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’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 “minute”, 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 “group by” returns the first rows “open” value, but obviously “close” 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)), ‘,’, -1)

from mytable

group by [xyz...]
—————————-

The group_concat() will return all “close” 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!

]]>
By: Zaman http://www.xaprb.com/blog/2007/08/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-14228 Zaman Thu, 21 Feb 2008 05:29:30 +0000 http://www.xaprb.com/blog/2007/03/21/how-to-select-the-first-or-last-row-per-group-in-sql/#comment-14228 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..

]]>