I know I am probably going about this the wrong way, but I am trying to understand Recursive CTE's.
I created a simple table
RowNum Type Amount
1 Anch 10
2 Amt 1
3 Amt 2
4 Amt 3
5 Amt 4
The idea was to anchor at the amount 10, the to recursively loop through and remove the amount from the total.
I came up with below
WITH cte_Rec (RowNum, [Type], Amount, Amount2, RT, RN)
AS (SELECT RowNum,
[Type],
Amount,
Amount,
Amount,
RowNum
FROM dbo.tbl_RecursiveCTE
WHERE [Type] = 'Anch'
UNION ALL
SELECT r.RowNum,
r.[Type],
r.Amount,
ct.Amount,
ct.Amount - r.Amount AS RT,
ct.RowNum
FROM dbo.tbl_RecursiveCTE r
INNER JOIN cte_Rec ct
ON ct.RowNum = r.RowNum - 1)
SELECT *
FROM cte_Rec
Which obv does not work.
Any ideas?