molivia03 0 Newbie Poster

Dear Dani web user,

Can anyone help me how to generate series of date that has start and end date and pick up the data along the way.

I have used Stored Procedure below but its not working

-- CREATE TEMP TABLE
declare @YTDbyMonth Table
(
--Period nvarchar(100)
DateStart datetime,
DateEnd datetime,
Purchases money,
Sales money,
)
declare 
		@StartDate as datetime,
		@EndDate as datetime,		
		@Purchase money,
		@Sales money,
		
WHILE (datediff(MONTH,@YTDStartDate,@YTDEndDate) > 0) 

	BEGIN
		-- reset valeue to zero
		SET @YTDStartDate = dateadd(MONTH,1,@YTDStartDate) 

		select @Purchase = 0
		select @Sales  = 0


				
		-- get purchase
		SELECT @Purchase = @Purchase + isnull(TraAmount,0) FROM TradeCurrent WHERE 
		(TraStaIDStatus = 25) AND --CompletedTransaction
		(TraMemIDBuyer = @MemID) AND 
		(TraDateExecuted BETWEEN @YTDStartDate AND @YTDEndDate)
		

		
		-- get sales
		SELECT @Sales = @Sales +isnull(TraAmount,0) FROM TradeCurrent WHERE 
		(TraStaIDStatus = 25) AND --CompletedTransaction
		(TraMemIDSeller = @MemID) AND 
		(TraDateExecuted BETWEEN @YTDStartDate AND @YTDEndDate)

		-- insert date in tableYTD
		insert into @YTDbyMonth
			values(@YTDStartDate,@YTDEndDate, @Purchase,@Sales)
			
END
--RETURN   
select * from @YTDbyMonth order by DateEnd ASC
END

for example:
start Date = 21 january 2009
End Date = 21 February 2010

The output would be:

date between                             Purchase     Sales
21 jan 2009 - 31 jan 2009                    0           0
1 Feb 2009 - 28 Feb 2009                     5           5
1 Apr 2009 - 31 Apr 2009                      1           0
.
.
.
1 feb 2010 - 21 Feb 2010                       0          5

Any help would be appreciated.

Thank you so much