A customer have a table with data stored by dates, and asked us to present data from this table by sequential date ranges.
This query sounded trivial but took us half a day to create such a select.
For simplicity consider a table of integer numbers, and try to build a select that returns pairs of continuous ranges of values.
So, for an input like this:
declare @values table ( value int not null primary key ); insert into @values(value) select 1 union all select 2 union all select 3 union all select 5 union all select 6 union all select 8 union all select 10 union all select 12 union all select 13 union all select 14;
You will have a following output:
low high ---- ---- 1 3 5 6 8 8 10 10 12 14
Logic of the algorithms is like this:
Following this logic we have built at least three different queries, where the shortest one is:
with source as ( select * from @values ) select l.value low, min(h.value) high from source l inner join source h on (l.value - 1 not in (select value from source)) and (h.value + 1 not in (select value from source)) and (h.value >= l.value) group by l.value;
Looking at this query it's hard to understand why it took so long to write so simple code...
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u