Raffaeu
Raffaeu

Reputation: 6973

T-SQL - Generate rows from Field with Number

I have a Table with the following Fields:

----------------------------------------
| Start Date        | Recurrences      |
----------------------------------------
| 01-01-2015        | 12               |
----------------------------------------
| 01-06-2015        | 10               |
----------------------------------------

What I need to output is a total of 22 rows (12 + 10). Each row should contain the 'Start Date' field plus the increment of days depending on which recurrence.

I was able to generate a number of rows with an increment using this table of SQL Server:

SELECT 
    DISTINCT n = number 
FROM 
    master..[spt_values] 
WHERE 
    number BETWEEN 1 AND 1000

But what I need is to trigger this select per each row of my table and set the MAX to Recurrences field and not to 1000

Upvotes: 0

Views: 73

Answers (1)

Luaan
Luaan

Reputation: 63752

Since you're multiplying, a simple inner join should do:

select
 Number,
 StartDate
from YourTable
inner join master..[spt_values] on number between 1 and YourTable.Recurrences

Upvotes: 2

Related Questions