Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a simple trick to collapse consecutive entries into a single group. If you group by (row_number - entry), the entries that are consecutive will end up in the same group. Here is an example demonstrating what I mean:</p> <p><strong>Query</strong>:</p> <pre><code>SELECT phonenum, @curRow := @curRow + 1 AS row_number, phonenum - @curRow from phonenums p join (SELECT @curRow := 0) r </code></pre> <p><strong>Results</strong>:</p> <pre><code>| PHONENUM | ROW_NUMBER | PHONENUM - @CURROW | ------------------------------------------------- | 27100070000 | 1 | 27100069999 | | 27100070001 | 2 | 27100069999 | | 27100070002 | 3 | 27100069999 | | 27100070003 | 4 | 27100069999 | | 27100070004 | 5 | 27100069999 | | 27100070005 | 6 | 27100069999 | | 27100070008 | 7 | 27100070001 | | 27100070009 | 8 | 27100070001 | | 27100070012 | 9 | 27100070003 | | 27100070015 | 10 | 27100070005 | | 27100070016 | 11 | 27100070005 | | 27100070040 | 12 | 27100070028 | </code></pre> <p>Notice how the entries that are consecutive all have the same value for <code>PHONENUM - @CURROW</code>. If we group on that column, and select the min &amp; max of each group, you have the summary (with one exception: you could replace the END value with <code>NULL</code> if START = END if that's a requirement):</p> <p><strong>Query</strong>:</p> <pre><code>select min(phonenum), max(phonenum) from ( SELECT phonenum, @curRow := @curRow + 1 AS row_number from phonenums p join (SELECT @curRow := 0) r ) p group by phonenum - row_number </code></pre> <p><strong>Results</strong>:</p> <pre><code>| MIN(PHONENUM) | MAX(PHONENUM) | --------------------------------- | 27100070000 | 27100070005 | | 27100070008 | 27100070009 | | 27100070012 | 27100070012 | | 27100070015 | 27100070016 | | 27100070040 | 27100070040 | </code></pre> <p>Demo: <a href="http://www.sqlfiddle.com/#!2/59b04/5">http://www.sqlfiddle.com/#!2/59b04/5</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload