Hi,
I try to do this with 2 solution method, but no luck:-
Example:
ID: 1
PostedDate: 2016-03-01,
Rate: 6 (month)
QPrice: 600
Output:
ID | PostedDate | Date | AmountByMth
1 | 2016-03-01 | 2016-03-01 | 100
1 | 2016-03-01 | 2016-04-01 | 100
1 | 2016-03-01 | 2016-05-01 | 100
1 | 2016-03-01 | 2016-06-01 | 100
1 | 2016-03-01 | 2016-07-01 | 100
1 | 2016-03-01 | 2016-08-01 | 100
Solution 1:
;WITH cte AS (
select
ID,
PostedDate=cast(convert(char(8), PostedDate, 112) + ' 00:00:00.000' as datetime),
DepRateByMth=m.Rate,
DepAmtByMth=QPrice/m.Rate
from tblMaster m
)
SELECT cte.ID, cte.PostedDate, [Date]=k.dateValue, AmountByMonth=cte.DepAmtByMth
FROM dbo.Fn_DateRange k
OPTION (MAXRECURSION 0)
Solution 2:
select * from
(
select
ID,
PostedDate=cast(convert(char(8), PostedDate, 112) + ' 00:00:00.000' as datetime),
DepRateByMth=m.Rate,
DepAmtByMth=QPrice/m.Rate
from tblMaster m
) k
CROSS JOIN dbo.Fn_DateRange
My Date Range Function as:
CREATE FUNCTION [dbo].[Fn_DateRange]
(@startDate AS DATE,
@interval AS INT,
@i int=0
)
RETURNS @Dates TABLE(dateValue DATE)
BEGIN
While @i < @interval
begin
insert into @Dates
SELECT DATEADD(month, @i, @startDate)
WHERE @i<@interval
Set @i = @i + 1
end
RETURN
END
Please advise.
Thank you.
Regards,
Micheale