so I have SQL table with many lines. It looks like this (just a couple lines from the top):
I need to disperse revenue according to project duration equally every 2 months starting first month. So desired result would look like this:
What’s the baest way to achieve this?
Advertisement
Answer
You can use recursive cte
:
with cte as ( select code, finish2 as acc_date, dateadd(month, project_duration, finish2) as end_date from table t union all select code, dateadd(month, 2, acc_date), end_date from cte c where dateadd(month, 2, acc_date) < end_date ) select code, acc_date from cte c;