Hi @ed_sas_member, Thanks for the quick response and trying to help me. Greatly appreciated. Here is the actual data set for combined scenarios 2.1,2.2 and 2.3 together. Data set for scenarios - 2.1,2.2 and 2.3: ID agent_id drug_code beg_date end_date effective_run_date 11 450 BC 11/1/2016 12/31/2016 11/2/2016 11 450 BD 11/1/2016 12/31/2016 12/2/2016 1 580 A1 1/1/2014 5/31/2018 8/29/2014 1 580 586 11/1/2017 11/30/2017 5/7/2018 1 580 567 12/1/2017 10/31/2018 5/7/2018 3 757 O1 8/1/2017 1/31/2019 6/26/2017 3 757 P1 8/1/2018 8/31/2018 8/3/2018 6 999 P1 9/1/2015 11/30/2016 10/20/2015 6 999 E1 10/1/2015 11/30/2015 10/20/2015 8 686 AL 1/1/2016 8/31/2016 1/4/2016 8 686 BL 6/1/2016 6/30/2016 1/23/2017 8 686 779 8/1/2016 8/31/2016 1/26/2017 8 686 775 8/1/2016 1/31/2017 1/12/2017 8 686 779 10/1/2016 1/31/2017 1/12/2017 9 99 BA 11/1/2015 12/31/2015 11/30/2015 9 99 CA 12/1/2015 12/31/2015 11/30/2015 10 699 F1 3/1/2015 12/31/2015 11/29/2016 10 699 BB 11/1/2015 3/31/2016 11/7/2016 Expected results: (I included comments and scenario type) ID agent_id drug_code beg_date end_date effective_run_date comments Scenario 11 450 BD 11/1/2016 12/31/2016 12/2/2016 I don’t want the first record since both records date ranges are same but effective date should be latest Scenario 2.1 1 580 A1 1/1/2014 10/31/2017 8/29/2014 (Changed the end date since the effective date is not latest compared to the other dates and I am not missing any unique months by changing the date). Scenario 2.2 1 580 586 11/1/2017 11/30/2017 5/7/2018 1 580 567 12/1/2017 10/31/2018 5/7/2018 3 757 O1 8/1/2017 7/31/2018 6/26/2017 I split the first record into two records since there is other reecord with one month with latest effective date. I am not losing any unique months again. Scenario 2.2 3 757 O1 9/1/2018 1/31/2019 6/26/2017 3 757 P1 8/1/2018 8/31/2018 8/3/2018 6 999 P1 9/1/2015 9/30/2015 10/20/2015 Here, when effective dates are same, I want to keep the overlap date ranges and split the non overlap months into three different records. Scenario 2.3 6 999 P1 10/1/2015 11/30/2015 10/20/2015 6 999 P1 12/1/2015 11/30/2016 10/20/2015 6 999 E1 10/1/2015 11/30/2015 10/20/2015 8 686 AL 1/1/2016 5/31/2016 1/4/2016 you can this case is three scenarios combines, first looking if they have same date ranges and removing the non latest effective date, changing the end dates based on the effective run dates keeping all the month date ranges and finally scenario 2.3 splitting the date ranges when they have same effective date ranges. combined scenarios 2.1,2.2 and 2.3 8 686 AL 7/1/2016 7/31/2016 1/4/2016 8 686 BL 6/1/2016 6/30/2016 1/23/2017 8 686 779 8/1/2016 8/31/2016 1/26/2017 8 686 775 9/1/2016 9/30/2017 1/12/2017 8 686 775 10/1/2016 1/31/2017 1/12/2017 8 686 779 10/1/2016 1/31/2017 1/12/2017 9 99 BA 11/1/2015 11/30/2015 11/30/2015 I am just splitting the first record into two date ranges since there is overlap and splitting non overlap date ranges since both have same effective run dates. Scenario 2.3 9 99 BA 12/1/2015 12/31/2015 11/30/2015 9 99 CA 12/1/2015 12/31/2015 11/30/2015 10 699 F1 3/1/2015 12/31/2015 11/29/2016 Here, second record date range is overlapped but since the effective run date is not latest, I changed it after the end date of the first record. Scenario 2.2 10 699 BB 1/1/2016 3/31/2016 11/7/2016
... View more