Comments on: How to select the Nth greatest/least/first/last row in SQL http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Geocine http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-18541 Geocine Thu, 05 Aug 2010 08:19:11 +0000 http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-18541 Hi how do I do this if for example my fruits table comes from a select result set?

]]>
By: Jordi Baylina http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-16695 Jordi Baylina Tue, 28 Jul 2009 18:54:47 +0000 http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-16695 select min(Time)
from speeds
where Time>Train_Arrival and Speed=0

]]>
By: Kumar http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-16692 Kumar Mon, 27 Jul 2009 16:37:13 +0000 http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-16692 A particular table in a database stores the time versus train speed data. This table keeps on growing with time as the train goes through a particular region (fixed in space).

Basically trains enter the region, move through it and then leave a region. I am pulling in GPS data for that region (time versus speed) each time a train goes through the region. So the table keeps on growing as a train arrives and leaves , and then the next train arrives and leaves and so on…

As the train goes through the region, it generates following type of data:

Time Speed
13:00 3 (say train enters the region at 13:00)
13:01 1
13:02 0
13:03 0
13:04 0
13:05 1
13:06 2
13:07 4
13:08 1
13:09 0
13:10 0
13:11 0
13:12 1
. .
. .
. .

The time the train enters the region is obviously not fixed. However another timestamp (say Train_Arrival from a difeerent table in the database) is available and it always indicates the latest train. (that is Train_Arrival timestamp will remain unchanged until the latest train has left the region and the next train arrives)

For the latest train (say in the above example, Train_Arrival was 12:55), I need to find the first time the train speed goes to ’0′ when inside the region (i.e. 13:02 in the above example).

It is not able to post process the data as the data is arriving in real-time and needs to be processed in real-time as well (a requirement for the application being built)

If you can provide me the right direction, it shall be deeply appreciated.

]]>
By: Jordi Baylina http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-15735 Jordi Baylina Mon, 26 Jan 2009 09:48:04 +0000 http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-15735 The main idea is to have a “calculated field” for each fruit that indicates the position in the group. This is the inner select.

Then, I just use this field to select the fruits that I am interested in order to make the sum, selection, or what ever.

]]>
By: Fred http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-15730 Fred Fri, 23 Jan 2009 15:34:15 +0000 http://www.xaprb.com/blog/2008/08/08/how-to-select-the-nth-greatestleastfirstlast-row-in-sql/#comment-15730 Thank you Jordi for the technique to handle identical entries. I needed to select the lowest four golf scores from the last 6 rounds played and couldn’t do it reliably until I saw your method.

If you revisit this site, perhaps you could explain how this works.

]]>