I have a budget table that lists projects, each with a number of entries listed by project ID, with individual start and end dates and a value.
I'm trying to create an additional table (or a view, but I'm not sure that's possible) which duplicates each row with a new entry for each year that the start date and end date encompasses, along with a simple sum to divide the line value by the number of days in that year. The sum bit works fine, and is not included here.
My problem is, I'm planning on inserting the new entries using the following code. However, this only works for one year and need to essentially run it five times to enter five different years into the same column. How would I go about inserting five years in one go?
declare @1415s datetime;
set @1415s = '2014-08-01';
declare @1415e datetime;
SET @1415e = '2015-07-31';
INSERT INTO [test]
(P_ID,startdate,enddate,Academic_Year)
SELECT
b.P_ID
CAST(b.Start_Date AS DATE) AS 'startdate',
CAST(b.End_Date AS DATE) AS 'enddate',
'Academic_Year'= CASE
WHEN startdate <= @1415s AND enddate <= @1415e AND enddate >= @1415s THEN '14/15'
WHEN startdate >= @1415s AND enddate <= @1415e THEN '14/15'
WHEN startdate <= @1415s AND enddate >= @1415e THEN '14/15'
WHEN startdate >= @1415s AND enddate >= @1415e AND startdate <= @1415e THEN '14/15'
END
FROM Budget b