Hello,I am working on a scenario where I would need to repeat the same row for previous MonthIDs, here's the example (snapshot attached):CustomerID MonthID RSCID Flag1 Flag2 Flag3123 294 3456 0 0 0123 300 1234 0 1 1123 303 3542 0 1 0123 303 2345 1 1 1123 304 3542 0 1 0123 304 2345 1 1 1123 305 3542 0 1 0123 305 2345 1 1 1123 306 2345 1 1 1123 306 3542 0 1 0123 307 2345 1 1 1123 307 3542 0 1 0123 308 3542 0 1 0123 309 3542 0 1 0 1. For MonthIDs 309 & 307, I would need to add same combinations to 4 prior months (for 309, I would need the 309 row as 308, 307, 306, 305 and for 307, I would need to add 306, 305, 304, 303).2. From the snapshot, Green rows are added for 309 MonthID and Orange rows are added for 307.3. While we are adding 309 row to prior to 4 months, if any prior month has different combination than 309, that same combination need to be repeated for prior months.I was using the below code to achieve the same:declare @SC_j as int = 0;declare @SC_k as int = 4;while @SC_j <= 4begin insert into #Test select distinct dt.CustomerID , dt.FiscalMonthID - @SC_j as FiscalMonthID , dt.RSCID , dt.Flag1 , dt.Flag2 , dt.Flag3 , NULL as NetPaidUnits from #Churn_STG_AdjustedLifeCycleUnit_Final as dt join #MonthPosition_SC as fl on dt.CustomerID = fl.CustomerID and dt.FiscalMonthID <= fl.FiscalMonthID and dt.FiscalMonthID >= fl.FiscalMonthID - @SC_k ; set @SC_j = @SC_j + 1; set @SC_k = @SC_k - 1;end;But my code starts from 1st MonthID however I would want this to be starting from last MonthID which I am not able to achieve. Request you to please look and let me know in case of any questions.Thank You!
↧