I have invoice records which are monthly, Annual, Bi-annual etc. I also have a field which provided number of months ( value will be 2 for monthly, 12 for annual etc.)
I need to convert annual invoice record in 12 monthly records and split the amount equally. Database is Snowflake
for ex:
Start_Date. No_of_Months. Amount. Frequency
1/07/2020 12 $120 Annual
11/23/2021 1 $16 Monthly
I want first record to split in 12 records with start date of (1/7/20, 2/7/20, 3/7/20 etc. and amount of $10 for each record). Second record will stay as is. If no. of months is 24 for some records, then that will split in 24 monthly records.
Solution:
I am planning to do it using a hardcoded cte with each month:
with cte(select 1 union select 2...select 60)
Select add_months(Start_Date,cte.each_month),Amount/No_of_Months
from table1
join cte
on table1.No_of_Months<=cte.each_month
Is there any better way of doing this. I am trying to avoid this hardcoded CTE.