I have table with columns with left and right value. Each row represent a section that starts on leftvalue and ends on rightvalue.
LeftValue   RightValue
----------- -----------
1           2
3           4
8           9
10          11
12          13
14          15
16          18
20          25
26          27
I want to optimize it to minimal number of contentious sections. For data above excepted result is
LeftValue   RightValue
----------- -----------
1           4
8           18
20          27
Some sections have been merged into one. For example 1-2 and 3-4 into 1-4.
Is there some efficient way to do this in sql? I did it with cursor by that approach is too slow.