Comments on: How to find contiguous ranges with SQL http://www.xaprb.com/blog/2006/03/22/find-contiguous-ranges-with-sql/ Stay curious! Mon, 13 May 2013 05:55:40 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: nathanm http://www.xaprb.com/blog/2006/03/22/find-contiguous-ranges-with-sql/#comment-20286 nathanm Tue, 25 Sep 2012 13:30:50 +0000 http://www.xaprb.com/blog/?p=119#comment-20286 love this query, do not understand the logic, and I’ve adapted it, and it works form. So much power from such few lines.

]]>
By: Greg Benison http://www.xaprb.com/blog/2006/03/22/find-contiguous-ranges-with-sql/#comment-20052 Greg Benison Mon, 14 May 2012 02:08:57 +0000 http://www.xaprb.com/blog/?p=119#comment-20052 I have described an approach that, instead of using “min()” within a subquery to join the “range start” and “range end” tables as done in this post, creates a synthetic index for each and joins on that.

http://gcbenison.wordpress.com/2011/09/26/queries-that-group-tables-by-contiguous-blocks/

I believe that for many large tables, the synthetic index approach will be faster but imposes an additional space requirement due to the added temporary table.

]]>
By: glitch http://www.xaprb.com/blog/2006/03/22/find-contiguous-ranges-with-sql/#comment-19791 glitch Sun, 18 Dec 2011 00:53:05 +0000 http://www.xaprb.com/blog/?p=119#comment-19791 Ezboy,
Yep that’s a TON faster… but it also doesn’t appear to give the range, just the next item in the list (unless I’m doing something wrong).

Anybody have any suggestions to make this more efficient on large data sets?

]]>
By: John http://www.xaprb.com/blog/2006/03/22/find-contiguous-ranges-with-sql/#comment-19276 John Tue, 12 Apr 2011 15:01:30 +0000 http://www.xaprb.com/blog/?p=119#comment-19276 This is so close to the solution I need, but I’m afraid a dreaded cursor is my only recourse. I have to identify groups of tickets in a range. Problem is I have multiple ranges within a given row. e.g.

Section Row Seat
112 G 1
112 G 2
112 G 3
112 G 4
112 G 9
112 G 10
112 G 11

Your sql correctly identifies 1 and 9 as the starting points for ranges and 4 and 11 but how do I then join back to the above table and correctly identify seat 3 in group 1 vs group 2? I need to be both less than the end of the range, and greater than the beginning.

Desired output
Section Row StarSeat EndSeat GroupID
112 G 1 4 1
112 G 9 11 2

or

Section Row Seat GroupID

112 G 1 1
112 G 2 1
112 G 3 1
112 G 4 1
112 G 9 2
112 G 10 2
112 G 11 2

Thanks,

John

]]>
By: EZboy http://www.xaprb.com/blog/2006/03/22/find-contiguous-ranges-with-sql/#comment-18921 EZboy Wed, 17 Nov 2010 15:58:54 +0000 http://www.xaprb.com/blog/?p=119#comment-18921 It looks like this query has very poor performance for large datasets. I found the inner select can be substituted by:

(select a.id as id from sequence a where a.id > l.id ORDER BY a.id ASC LIMIT 1) as end

This will greatly improve performance

]]>