0

I am using sql server and this is my table structure

start end interval
1     3   1
9     12  1
16    20  2
100   120 5

expected result

1
2
3
9
10
11
12
16
18
20
100
105
110
115
120

i tried this before posting here

select start as result,end1,interval
from table
union
select result+1,interval,end1,interval 
from table
1
  • 1
    What is the logic you were trying to write? How is the expected result is calculated? Commented Mar 6, 2014 at 14:14

1 Answer 1

2

This is a perfect place to use a CTE. The following code should give you the answer you're looking for:

;WITH IntervalCTE AS
(
  SELECT [start] AS Value, [end], [interval]
  FROM T
  UNION ALL
  SELECT [Value] + [interval], [end], [interval]
  FROM IntervalCTE
  WHERE [Value] < [end]
)
SELECT Value FROM IntervalCTE ORDER BY Value

I've also created a SQL Fiddle that you can look at.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.