Generate a range of date and compare with another range of date and pivot it

Viewed 2363

I have a situation where I have to generate a range of date (say start date and end date). Now I want to fill the records of specific dates on those generated dates.

;WITH DateRange AS
(
    SELECT @start_date DateValue
    UNION ALL
    SELECT DateValue + 1
    FROM   DateRange
    WHERE  DateValue + 1 <= @end_date
)

If @start_date = '2013-01-01' and @end_date= '2013-01-05'

The dates generated will be '01/01/2013,01/02/2013,01/03/2013,01/04/2013,01/05/2013'

Now I want to pivot it to fill the specific data for specific date. How can I achieve this?

Edit: I have 3 columns namely Duration, StartDate and EndDate. Now I want to generate these dates dynamically and fill the values of Duration accourdingly.

For eg: if Duration = 6 and @start_date = '2013-01-01' and @end_date= '2013-01-05' I want to generate a list of dates with duration equal to 6.

Update 2:

If you haven't understand my question here's a full detail for it.

  1. I have to pass @startDate and @EndDate as parameters
  2. I have fields Id, StartDate and EndDate, duration and other fields in my table
  3. Now when I pass the parameters the query should generate the range of dates from @startDate and @EndDate
  4. As the dates are generated it must check the dates StartDate and EndDate in the table and set the value of Duration to those fields from StartDate to EndDate.

enter image description here

Required output: If @startDate='2013-01-01' and @endDate='2013-01-07' then the output must be like this:

enter image description here

**

Note: the Id for both the rows in first image is same.

**

Update 3:

enter image description here enter image description here

3 Answers
Related