text: {DEFAULT @CDM_schema = 'Alligator_CDM4_All'}
  {DEFAULT @HTN_Combi = 'SJ_HTN_combi'}
  {DEFAULT @HTN_Combi_code = 'Chan_HTN_Combi'}
  
  USE [@HTN_combi];
  
  -- OVERLAP DATE OF DRUG A AND DRUG C AND DRUG D
  --(5283398 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].OVERLAP_DATE_ACD', 'U') IS NOT NULL
  	DROP TABLE OVERLAP_DATE_ACD;
  SELECT A.DATES, B.PERSON_ID
  	, B.DRUG_CLASS_SIMPLE DRUG_CLASS_SIMPLE_1, B.DRUG_FU_SEQ DRUG_FU_SEQ_1
  	, C.DRUG_CLASS_SIMPLE DRUG_CLASS_SIMPLE_2, C.DRUG_FU_SEQ DRUG_FU_SEQ_2
  	, D.DRUG_CLASS_SIMPLE DRUG_CLASS_SIMPLE_3, D.DRUG_FU_SEQ DRUG_FU_SEQ_3
  INTO OVERLAP_DATE_ACD
  FROM ALL_DATES A
  	INNER JOIN (
  		SELECT PERSON_ID, DRUG_CLASS_SIMPLE, MIN_START_DATE, MAX_END_DATE, LAST_FU_DATE, DRUG_FU_SEQ FROM DRUG_HISTORY_S7D_G14D_C90D_FU WHERE DRUG_CLASS_SIMPLE IN ('A')
  	) B
  	ON A.DATES BETWEEN B.MIN_START_DATE AND B.MAX_END_DATE
  	INNER JOIN (
  		SELECT PERSON_ID, DRUG_CLASS_SIMPLE, MIN_START_DATE, MAX_END_DATE, LAST_FU_DATE, DRUG_FU_SEQ FROM DRUG_HISTORY_S7D_G14D_C90D_FU WHERE DRUG_CLASS_SIMPLE IN ('C')
  	) C
  	ON B.PERSON_ID = C.PERSON_ID
  		AND A.DATES BETWEEN C.MIN_START_DATE AND C.MAX_END_DATE
  	INNER JOIN (
  		SELECT PERSON_ID, DRUG_CLASS_SIMPLE, MIN_START_DATE, MAX_END_DATE, LAST_FU_DATE, DRUG_FU_SEQ FROM DRUG_HISTORY_S7D_G14D_C90D_FU WHERE DRUG_CLASS_SIMPLE IN ('D')
  	) D
  	ON B.PERSON_ID = D.PERSON_ID
  		AND A.DATES BETWEEN D.MIN_START_DATE AND D.MAX_END_DATE
  
  SELECT * FROM OVERLAP_DATE_ACD ORDER BY PERSON_ID, DATES, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2, DRUG_CLASS_SIMPLE_3, DRUG_FU_SEQ_3
  
  -- OVERLAP DATE OF (DRUG AC - DRUG ACD)
  --(7872919 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].OVERLAP_DATE_AC_ACD', 'U') IS NOT NULL
  	DROP TABLE OVERLAP_DATE_AC_ACD;
  SELECT A.*
  INTO OVERLAP_DATE_AC_ACD
  FROM OVERLAP_DATE_AC A
  	INNER JOIN (
  		SELECT DATES, PERSON_ID FROM OVERLAP_DATE_AC
  		EXCEPT
  		SELECT DATES, PERSON_ID FROM OVERLAP_DATE_ACD
  	) B
  	ON A.DATES = B.DATES
  		AND A.PERSON_ID = B.PERSON_ID
  
  SELECT * FROM OVERLAP_DATE_AC_ACD ORDER BY PERSON_ID, DATES, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- OVERLAP DATE OF (DRUG AD - DRUG ACD)
  --(10325040 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].OVERLAP_DATE_AD_ACD', 'U') IS NOT NULL
  	DROP TABLE OVERLAP_DATE_AD_ACD;
  SELECT A.*
  INTO OVERLAP_DATE_AD_ACD
  FROM OVERLAP_DATE_AD A
  	INNER JOIN (
  		SELECT DATES, PERSON_ID FROM OVERLAP_DATE_AD
  		EXCEPT
  		SELECT DATES, PERSON_ID FROM OVERLAP_DATE_ACD
  	) B
  	ON A.DATES = B.DATES
  		AND A.PERSON_ID = B.PERSON_ID
  
  SELECT * FROM OVERLAP_DATE_AD_ACD ORDER BY PERSON_ID, DATES, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- MULTI DRUG DATE OF (DRUG AC - DRUG ACD)
  --(21964 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].MULTI_2_DRUG_DATE_AC_ACD', 'U') IS NOT NULL
  	DROP TABLE MULTI_2_DRUG_DATE_AC_ACD;
  SELECT PERSON_ID, LAST_FU_DATE, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  	, MIN(DATES) MIN_DATE
  	, MAX(DATES) MAX_DATE
  INTO MULTI_2_DRUG_DATE_AC_ACD
  FROM OVERLAP_DATE_AC_ACD
  GROUP BY PERSON_ID, LAST_FU_DATE, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  --ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  SELECT * FROM MULTI_2_DRUG_DATE_AC_ACD ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- MULTI DRUG DATE OF (DRUG AD - DRUG ACD)
  --(27536 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].MULTI_2_DRUG_DATE_AD_ACD', 'U') IS NOT NULL
  	DROP TABLE MULTI_2_DRUG_DATE_AD_ACD;
  SELECT PERSON_ID, LAST_FU_DATE, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  	, MIN(DATES) MIN_DATE
  	, MAX(DATES) MAX_DATE
  INTO MULTI_2_DRUG_DATE_AD_ACD
  FROM OVERLAP_DATE_AD_ACD
  GROUP BY PERSON_ID, LAST_FU_DATE, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  --ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  SELECT * FROM MULTI_2_DRUG_DATE_AD_ACD ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- MULTI DRUG DATE DAYS OF (DRUG AC - DRUG ACD)
  --(21964 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].MULTI_2_DRUG_DATE_DAYS_AC_ACD', 'U') IS NOT NULL
  	DROP TABLE MULTI_2_DRUG_DATE_DAYS_AC_ACD;
  SELECT *, DATEDIFF(DAY, MIN_DATE, MAX_DATE) CONT_DAYS
  INTO MULTI_2_DRUG_DATE_DAYS_AC_ACD
  FROM MULTI_2_DRUG_DATE_AC_ACD
  --ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  SELECT * FROM MULTI_2_DRUG_DATE_DAYS_AC_ACD ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- MULTI DRUG DATE DAYS OF (DRUG AD - DRUG ACD)
  --(27536 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].MULTI_2_DRUG_DATE_DAYS_AD_ACD', 'U') IS NOT NULL
  	DROP TABLE MULTI_2_DRUG_DATE_DAYS_AD_ACD;
  SELECT *, DATEDIFF(DAY, MIN_DATE, MAX_DATE) CONT_DAYS
  INTO MULTI_2_DRUG_DATE_DAYS_AD_ACD
  FROM MULTI_2_DRUG_DATE_AD_ACD
  --ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  SELECT * FROM MULTI_2_DRUG_DATE_DAYS_AD_ACD ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- MULTI DRUG DATE CONTINUOUS 90 DAYS OF (DRUG AC - DRUG ACD)
  --(20340 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].MULTI_2_DRUG_DATE_DAYS_C90D_AC_ACD', 'U') IS NOT NULL
  	DROP TABLE MULTI_2_DRUG_DATE_DAYS_C90D_AC_ACD;
  SELECT *
  INTO MULTI_2_DRUG_DATE_DAYS_C90D_AC_ACD
  FROM MULTI_2_DRUG_DATE_DAYS_AC_ACD
  WHERE CONT_DAYS>=90
  --ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  SELECT * FROM MULTI_2_DRUG_DATE_DAYS_C90D_AC_ACD ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  -- MULTI DRUG DATE CONTINUOUS 90 DAYS OF (DRUG AD - DRUG ACD)
  --(26405 row(s) affected)
  IF OBJECT_ID('[@HTN_combi].[dbo].MULTI_2_DRUG_DATE_DAYS_C90D_AD_ACD', 'U') IS NOT NULL
  	DROP TABLE MULTI_2_DRUG_DATE_DAYS_C90D_AD_ACD;
  SELECT *
  INTO MULTI_2_DRUG_DATE_DAYS_C90D_AD_ACD
  FROM MULTI_2_DRUG_DATE_DAYS_AD_ACD
  WHERE CONT_DAYS>=90
  --ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  
  SELECT * FROM MULTI_2_DRUG_DATE_DAYS_C90D_AD_ACD ORDER BY PERSON_ID, DRUG_CLASS_SIMPLE_1, DRUG_FU_SEQ_1, DRUG_CLASS_SIMPLE_2, DRUG_FU_SEQ_2
  