Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a <a href="http://www.sqlservercentral.com/articles/T-SQL/62867/" rel="nofollow noreferrer" title="The &quot;Numbers&quot; or &quot;Tally&quot; Table: What it is and how it replaces a loop.">number table</a> to solve this problem. Basically, you first expand the ranges, then combine subsequent items in groups.</p> <p>Here's one implementation:</p> <pre><code>WITH data (START, [END]) AS ( SELECT 1, 8 UNION ALL SELECT 9, 13 UNION ALL SELECT 14, 20 UNION ALL SELECT 20, 25 UNION ALL SELECT 30, 42 UNION ALL SELECT 42, 49 UNION ALL SELECT 60, 67 ), expanded AS ( SELECT DISTINCT N = d.START + v.number FROM data d INNER JOIN master..spt_values v ON v.number BETWEEN 0 AND d.[END] - d.START WHERE v.type = 'P' ), marked AS ( SELECT N, SeqID = N - ROW_NUMBER() OVER (ORDER BY N) FROM expanded ) SELECT START = MIN(N), [END] = MAX(N) FROM marked GROUP BY SeqID </code></pre> <p>This solution uses <a href="https://stackoverflow.com/questions/4273723/what-is-the-purpose-of-system-table-table-master-spt-values-and-what-are-the-mea" title="What is the purpose of system table table master..spt_values and what are the meanings of its values?"><code>master..spt_values</code></a> as a number table, for expanding the initial ranges. But if (all or some of) those ranges may span more than 2048 (subsequent) values, then you should define and use <a href="https://stackoverflow.com/questions/10819/sql-auxiliary-table-of-numbers" title="SQL, Auxiliary table of numbers">your own</a> number table.</p>
    singulars
    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. 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.
    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