SQL Server 2008: Loop/Iterate through date range while inserting

I have a scenario where I have to break the dataset down by the month and insert the results of the breakdown into a new table.

The data looks like this:
SUB_ID|EFF_DTE|TERM_DTE|PRO_ID|MD_NUM|AID
00010020100|2011-05-01|2011-08-31|1040160053|MK332|BSD            
00010020100|2012-01-01|2012-06-30|1040160044|FK172|ASM

The result needs to look like this for the first record, the range is from May to August

MONTH|PRO_ID|MD_NUM|AID
2011-05-01|1040160053|MK332|BSD
2011-06-01|1040160053|MK332|BSD
2011-07-01|1040160053|MK332|BSD
2011-08-01|1040160053|MK332|BSD
2011-05-01|1040160053|MK332|BSD

Then the data for the SUB_ID record changes and the new range is January to June, the results need to look like this:

2012-01-01|1040160044|FK172|ASM
2012-02-01|1040160044|FK172|ASM
2012-03-01|1040160044|FK172|ASM
2012-04-01|1040160044|FK172|ASM
2012-05-01|1040160044|FK172|ASM
2012-06-01|1040160044|FK172|ASM

Here's my code.

WITH RecCTE AS (
  SELECT sub_id, eff_dte
  FROM rawdata
  UNION ALL
  SELECT R.sub_id, DATEADD(day,1,R.eff_dte)
  FROM RecCTE R JOIN rawdata RD ON R.eff_dte < RD.term_dte
  )
INSERT INTO SDATA
SELECT R.sub_id, R.eff_dte
FROM RecCTE R
  JOIN (SELECT sub_id, pro_id, md_num,aid
        FROM rawdata
       ) T ON R.empid = T.empid
  JOIN (SELECT sub_id, COUNT(*) cnt
        FROM RecCTE
        GROUP BY sub_id) R2 ON R.sub_id=R2.sub_id
SharonBernalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give the below code a whirl to create a table with the the first day of the month for whatever range you want, then you can do a query with this table LEFT JOIN your data to return a set that is guaranteed to include the first day of the month.
-- Set your start date, end date, and number of days
Declare @StartDt date = '2013-03-01'
Declare @EndDt date = DATEADD(M, 1, @StartDt)
declare @Days int = DATEDIFF(d, @StartDt, @EndDt) 

-- TESTING ONLY
SELECT @StartDt, @EndDt, @Days

Declare @i int = 0

WHILE @i < @Days
	begin

	INSERT INTO YourTable (DayOfMonth, Location) 
	SELECT DATEADD(d, @i, @StartDt), Location
	FROM the_location_table
	
	SET @i = @i + 1
	
	END

Open in new window

Also, if it helps, I wrote an article on How to create a calendar table that has some looping in it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J DeppCommented:
Great example, aided me in understanding iteration in sql server!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.