From b3d6223f455ceabf8021c7aad59861b340669de3 Mon Sep 17 00:00:00 2001
From: "Hubbell, Harrison Taylor" <hhubbell@partners.org>
Date: Thu, 22 Apr 2021 08:34:00 -0400
Subject: [PATCH 1/5] Add UNITS to strength conversion list

---
 medication_days_covered/medication_days_covered.sql | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/medication_days_covered/medication_days_covered.sql b/medication_days_covered/medication_days_covered.sql
index ae466ea..9885fb1 100755
--- a/medication_days_covered/medication_days_covered.sql
+++ b/medication_days_covered/medication_days_covered.sql
@@ -44,12 +44,12 @@ select
     fm.FrequencyHoursNBR as CalculatedFrequencyHoursNBR,
     coalesce(convert(NUMERIC(18, 4), substring(ord.DiscreteDoseAMT, charindex('-', ord.DiscreteDoseAMT) + 1, len(ord.DiscreteDoseAMT))), 1) as CalculatedUnitsPerDoseAMT,
     case
-        when ord.DoseUnitDSC in ('MG', 'MCG', 'ML') then ms.StrengthAMT
+        when ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS') then ms.StrengthAMT
         else 1.0000
     end as CalculatedStrengthAMT,
     ceiling(ord.DiscreteDispenseQuantityNBR / ((
         coalesce(convert(NUMERIC(18, 4), substring(ord.DiscreteDoseAMT, charindex('-', ord.DiscreteDoseAMT) + 1, len(ord.DiscreteDoseAMT))), 1.0000)
-                / case when ord.DoseUnitDSC in ('MG', 'MCG', 'ML') then ms.StrengthAMT else 1.0000 end)
+                / case when ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS') then ms.StrengthAMT else 1.0000 end)
                     * (24 / fm.FrequencyHoursNBR))) as CalculatedDaysSupplyNBR
 from Epic.Orders.Medication_Enterprise ord
     inner join Epic.Patient.Patient_Enterprise ptn on ord.PatientID = ptn.PatientID
-- 
GitLab


From 9cfe212f5d96bb9f5f82434757fe909b81c754b7 Mon Sep 17 00:00:00 2001
From: "Hubbell, Harrison Taylor" <hhubbell@partners.org>
Date: Thu, 22 Apr 2021 08:36:23 -0400
Subject: [PATCH 2/5] replace case with iif() one-liner

---
 medication_days_covered/medication_days_covered.sql | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/medication_days_covered/medication_days_covered.sql b/medication_days_covered/medication_days_covered.sql
index 9885fb1..ec9d678 100755
--- a/medication_days_covered/medication_days_covered.sql
+++ b/medication_days_covered/medication_days_covered.sql
@@ -43,14 +43,10 @@ select
     /* Calculated Fields */
     fm.FrequencyHoursNBR as CalculatedFrequencyHoursNBR,
     coalesce(convert(NUMERIC(18, 4), substring(ord.DiscreteDoseAMT, charindex('-', ord.DiscreteDoseAMT) + 1, len(ord.DiscreteDoseAMT))), 1) as CalculatedUnitsPerDoseAMT,
-    case
-        when ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS') then ms.StrengthAMT
-        else 1.0000
-    end as CalculatedStrengthAMT,
+    iif(ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS'), ms.StrengthAMT, 1.0000) as CalculatedStrengthAMT,
     ceiling(ord.DiscreteDispenseQuantityNBR / ((
         coalesce(convert(NUMERIC(18, 4), substring(ord.DiscreteDoseAMT, charindex('-', ord.DiscreteDoseAMT) + 1, len(ord.DiscreteDoseAMT))), 1.0000)
-                / case when ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS') then ms.StrengthAMT else 1.0000 end)
-                    * (24 / fm.FrequencyHoursNBR))) as CalculatedDaysSupplyNBR
+                / iif(ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS'), ms.StrengthAMT, 1.0000)) * (24 / fm.FrequencyHoursNBR))) as CalculatedDaysSupplyNBR
 from Epic.Orders.Medication_Enterprise ord
     inner join Epic.Patient.Patient_Enterprise ptn on ord.PatientID = ptn.PatientID
     inner join Epic.Reference.Medication med on ord.MedicationID = med.MedicationID
-- 
GitLab


From bc3be74f3fa31004171c2e130cb43c5cc5bedaac Mon Sep 17 00:00:00 2001
From: "Hubbell, Harrison Taylor" <hhubbell@partners.org>
Date: Thu, 22 Apr 2021 13:18:33 -0400
Subject: [PATCH 3/5] Improve days covered calculation for orders requiring
 unit conversion

---
 .../medication_days_covered.sql               | 90 +++++++++++++------
 1 file changed, 65 insertions(+), 25 deletions(-)

diff --git a/medication_days_covered/medication_days_covered.sql b/medication_days_covered/medication_days_covered.sql
index ec9d678..e400dee 100755
--- a/medication_days_covered/medication_days_covered.sql
+++ b/medication_days_covered/medication_days_covered.sql
@@ -1,6 +1,7 @@
 --
--- Author:      Harrison Hubbell <hhubbell@partners.org>
--- Reference:   Lucero Leon-Chi <lleon-chi@partners.org>
+-- Author:      Lucero Leon-Chi <lleon-chi@partners.org>
+--              Harrison Hubbell <hhubbell@partners.org>
+--              Jim Dziobek <jdziobek@partners.org>
 -- Description: Determine days covered for a medication order
 --
 
@@ -10,16 +11,35 @@ with medication_strength as (
     select
         MedicationID,
         StrengthAMT as StrengthTXT,
-        -- First occurance of non-numeric value
-        patindex('%[^0-9.,]%', StrengthAMT) as CharIndexNBR,
-        try_convert(NUMERIC(18, 4), case
-            when patindex('%[^0-9.,]%', StrengthAMT) = 0 then replace(StrengthAMT, ',', '')
-            else replace(substring(StrengthAMT, 1, patindex('%[^0-9.,]%', StrengthAMT) - 1), ',', '')
-        end) as StrengthAMT
+        value as StrengthSplitTXT,
+        /*
+         * Convert the numeric portion of `value` to the numeric strength coefficient
+         *  IF:     No characters in string, return full cleaned numeric value
+         *  IF:     Only characters in string, return '1'
+         *  ELSE:   Subset the string to just the numeric part
+         */
+        convert(NUMERIC(18, 4),
+            case
+                when patindex('%[^0-9.,]%', value) = 0 then replace(value, ',', '')
+                when patindex('%[^0-9.,]%', value) = 1 then '1'
+                else replace(substring(value, 1, patindex('%[^0-9.,]%', value) - 1), ',', '')
+            end) as StrengthAMT,
+        /*
+         * Convert the character portion of `value` to the strength unit
+         *  IF:     Only characters in string, return full string
+         *  ELSE:   Subset the string to just the character part and clean
+         */
+        convert(VARCHAR,
+            case
+                when patindex('%[^0-9.,-]%', value) = 1 then value
+                when charindex(' ', ltrim(substring(value, patindex('%[^0-9.,-]%', value), len(value)))) > 0
+                    then ltrim(substring(value, patindex('%[^0-9.,-]%', value), charindex(' ', ltrim(substring(value, patindex('%[^0-9.,-]%', value), len(value))))))
+                else ltrim(substring(value, patindex('%[^0-9.,-]%', value), len(value)))
+        end) as StrengthUnitDSC
     from Epic.Reference.Medication
+        cross apply string_split(StrengthAMT, '/')
     where StrengthAMT is not null
-        -- Problem Medication ID
-        and MedicationID != 40840042
+        and value != ''
 )
 select
     ptn.EDWPatientID,
@@ -27,32 +47,52 @@ select
     ord.PatientEncounterID,
     ord.OrderID,
     ord.OrderDTS,
+    ord.PrescribingOrAuthorizingProviderID as PrescribingProviderID,
     ser2.NPI as PrescribingProviderNPI,
     ord.MedicationID,
-    med.MedicationDSC as MedicationNM,
+    ord.MedicationNM,
     ord.SigTXT,
     ord.DiscreteFrequencyID as FrequencyID,
     ord.DiscreteFrequencyDSC as FrequencyDSC,
     ord.DiscreteDispenseQuantityNBR as DispenseQTY,
-    ord.DiscreteDoseAMT as DoseAMT,
+    disp_st.StrengthAMT as DispenseStrengthAMT,
     ord.DiscreteDispenseUnitDSC as DispenseUnitDSC,
+    ord.DoseAMT,
+    dose_st.StrengthAMT as DostStrengthAMT,
     upper(ord.DoseUnitDSC) as DoseUnitDSC,
-    ms.StrengthTXT,
-    ms.StrengthAMT,
-    med.TherapeuticClassDSC,
     /* Calculated Fields */
     fm.FrequencyHoursNBR as CalculatedFrequencyHoursNBR,
-    coalesce(convert(NUMERIC(18, 4), substring(ord.DiscreteDoseAMT, charindex('-', ord.DiscreteDoseAMT) + 1, len(ord.DiscreteDoseAMT))), 1) as CalculatedUnitsPerDoseAMT,
-    iif(ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS'), ms.StrengthAMT, 1.0000) as CalculatedStrengthAMT,
-    ceiling(ord.DiscreteDispenseQuantityNBR / ((
-        coalesce(convert(NUMERIC(18, 4), substring(ord.DiscreteDoseAMT, charindex('-', ord.DiscreteDoseAMT) + 1, len(ord.DiscreteDoseAMT))), 1.0000)
-                / iif(ord.DoseUnitDSC in ('MG', 'MCG', 'ML', 'UNITS'), ms.StrengthAMT, 1.0000)) * (24 / fm.FrequencyHoursNBR))) as CalculatedDaysSupplyNBR
-from Epic.Orders.Medication_Enterprise ord
+    (ord.DiscreteDispenseQuantityNBR / coalesce(disp_st.StrengthAMT, 1.000))
+        / (ord.DoseAMT / coalesce(dose_st.StrengthAMT, 1.0000)) as CalculatedDosesNBR,
+    ceiling((ord.DiscreteDispenseQuantityNBR / coalesce(disp_st.StrengthAMT, 1.000))
+        / (ord.DoseAMT / coalesce(dose_st.StrengthAMT, 1.0000))
+        / (24 / fm.FrequencyHoursNBR)) as CalculatedDaysCoveredNBR
+from (
+    select
+        PatientID,
+        PatientEncounterID,
+        OrderID,
+        OrderDTS,
+        MedicationID,
+        MedicationDSC as MedicationNM,
+        PrescribingOrAuthorizingProviderID,
+        SigTXT,
+        DiscreteFrequencyID,
+        DiscreteFrequencyDSC,
+        DiscreteDispenseQuantityNBR,
+        DiscreteDispenseUnitDSC,
+        DiscreteDoseAMT as DoseTXT,
+        coalesce(convert(NUMERIC(18, 4), substring(DiscreteDoseAMT, charindex('-', DiscreteDoseAMT) + 1, len(DiscreteDoseAMT))), 1.0000) as DoseAMT,
+        DoseUnitDSC,
+        OrderingModeCD
+    from Epic.Orders.Medication_Enterprise
+) ord
     inner join Epic.Patient.Patient_Enterprise ptn on ord.PatientID = ptn.PatientID
-    inner join Epic.Reference.Medication med on ord.MedicationID = med.MedicationID
-    left join medication_strength ms on med.MedicationID = ms.MedicationID
-        and ms.StrengthAMT > 0
-    left join Epic.Reference.Resource2 ser2 on ord.MedicationPrescribingProviderID = ser2.ProviderID
+    left join medication_strength disp_st on ord.MedicationID = disp_st.MedicationID
+        and ord.DiscreteDispenseUnitDSC = disp_st.StrengthUnitDSC
+    left join medication_strength dose_st on ord.MedicationID = dose_st.MedicationID
+        and ord.DoseUnitDSC = dose_st.StrengthUnitDSC
+    left join Epic.Reference.Resource2 ser2 on ord.PrescribingOrAuthorizingProviderID = ser2.ProviderID
     left join UserWork.EAViewLibrary.ReferenceMapFrequency fm on ord.DiscreteFrequencyID = fm.FrequencyID
 -- Outpatient orders only
 where ord.OrderingModeCD = 1
-- 
GitLab


From 937d0abc402507658c6d92a66b93cbdcfd78b279 Mon Sep 17 00:00:00 2001
From: "Hubbell, Harrison Taylor" <hhubbell@partners.org>
Date: Thu, 22 Apr 2021 13:24:25 -0400
Subject: [PATCH 4/5] Fix typo

---
 medication_days_covered/medication_days_covered.sql | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/medication_days_covered/medication_days_covered.sql b/medication_days_covered/medication_days_covered.sql
index e400dee..6a2bbbf 100755
--- a/medication_days_covered/medication_days_covered.sql
+++ b/medication_days_covered/medication_days_covered.sql
@@ -58,7 +58,7 @@ select
     disp_st.StrengthAMT as DispenseStrengthAMT,
     ord.DiscreteDispenseUnitDSC as DispenseUnitDSC,
     ord.DoseAMT,
-    dose_st.StrengthAMT as DostStrengthAMT,
+    dose_st.StrengthAMT as DoseStrengthAMT,
     upper(ord.DoseUnitDSC) as DoseUnitDSC,
     /* Calculated Fields */
     fm.FrequencyHoursNBR as CalculatedFrequencyHoursNBR,
-- 
GitLab


From ac036ff68a6ea62c14a26fa722a82c5dc6608f8a Mon Sep 17 00:00:00 2001
From: "Hubbell, Harrison Taylor" <hhubbell@partners.org>
Date: Thu, 22 Apr 2021 14:50:38 -0400
Subject: [PATCH 5/5] Update frequency table

---
 .../reference_map_frequency.sql               | 54 ++++++++++---------
 1 file changed, 28 insertions(+), 26 deletions(-)

diff --git a/reference_map_frequency/reference_map_frequency.sql b/reference_map_frequency/reference_map_frequency.sql
index e1f423f..ae73277 100644
--- a/reference_map_frequency/reference_map_frequency.sql
+++ b/reference_map_frequency/reference_map_frequency.sql
@@ -48,38 +48,40 @@ select
             then 6
         when FrequencyID in ('124', '100007', '100014', '100015', '100021',
                 '100029', '100067', '100069', '100080', '100085', '100152',
-                '100157', '100177', '100216', '200011', '200045', '200046',
-                '200061', '200521', '200527', '200552', '200565', '200801',
-                '200811', '200933', '200934', '200941', '200958', '304010',
-                '304101', '304116', '304140000', '304140104', '3043300300',
-                '304490328')
+                '100157', '100177', '100216', '100508', '100509', '200011',
+                '200045', '200046', '200061', '200521', '200527', '200552',
+                '200565', '200801', '200811', '200933', '200934', '200941',
+                '200958', '304010', '304101', '304116', '304140000',
+                '304140104', '3043300300', '304490328')
             then 8
         when FrequencyID in ('100078') then 10
-        when FrequencyID in ('107', '100005', '100009', '100011', '100027',
-                '100052', '100056', '100070', '200006', '200009', '200056',
-                '200062', '200523', '200528', '200551', '200576', '200800',
-                '200804', '200937', '304100', '304125', '304140105',
-                '304140107')
+        when FrequencyID in ('107', '168', '100005', '100009', '100011',
+                '100027', '100052', '100056', '100070', '200006', '200009',
+                '200056', '200062', '200523', '200528', '200551', '200576',
+                '200800', '200804', '200937', '304100', '304125', '333000',
+                '304140105', '304140107')
             then 12
         when FrequencyID in ('100071', '200577', '200920') then 16
         when FrequencyID in ('200590', '200929') then 18
         when FrequencyID in ('200585') then 20
         when FrequencyID in ('200578') then 22
         when FrequencyID in ('200579', '200584') then 23
-        when FrequencyID in ('20', '41', '122', '100000', '100012', '100024',
-                '100026', '100036', '100053', '100055', '100073', '100092',
-                '100094', '100095', '100121', '100139', '200001', '200005',
-                '200023', '200025', '200051', '200054', '200058', '200063',
-                '200502', '200507', '200544', '200550', '200556', '200557',
-                '200560', '200561', '200562', '200563', '200570', '200803',
-                '200812', '200902', '200939', '200913', '200952', '200955',
-                '200947', '200961', '200936', '200940', '304102', '304104',
-                '304112', '304113', '304120', '10002401', '304140001',
-                '304140106', '304140113')
+        when FrequencyID in ('20', '41', '122', '27990', '100000', '100012',
+                '100024', '100026', '100036', '100053', '100055', '100073',
+                '100092', '100094', '100095', '100121', '100139', '100502',
+                '100503', '100504', '100505', '100506', '100507', '200001',
+                '200005', '200022', '200023', '200025', '200051', '200054',
+                '200058', '200063', '200502', '200507', '200544', '200550',
+                '200556', '200557', '200560', '200561', '200562', '200563',
+                '200570', '200803', '200812', '200902', '200939', '200913',
+                '200952', '200955', '200947', '200961', '200936', '200940', 
+                '304102', '304104', '304112', '304113', '304120', '400001',
+                '2200001', '10002401', '304140001', '304140106', '304140113')
             then 24
         when FrequencyID in ('55', '200945') then 33.6
         when FrequencyID in ('100075', '200928') then 36
         when FrequencyID in ('200923') then 42
+        when FrequencyID in ('200592') then 44
         when FrequencyID in ('200580') then 46
         when FrequencyID in ('100023', '100044', '100304', '100501', '200510',
                 '200529', '200571', '200925', '200932', '304140110')
@@ -97,14 +99,14 @@ select
         when FrequencyID in ('200581') then 108
         when FrequencyID in ('200574') then 120
         when FrequencyID in ('200582') then 144
-        when FrequencyID in ('11', '100087', '100164', '100165', '100166',
-                '100167', '100168', '100169', '100170', '200535', '200583',
-                '200924', '304103', '304114', '304115', '304118', '304121',
-                '304126', '304127', '304128')
+        when FrequencyID in ('11', '159', '27991', '100087', '100164', '100165',
+                '100166', '100167', '100168', '100169', '100170', '200535',
+                '200583', '200924', '304103', '304114', '304115', '304118',
+                '304121', '304126', '304127', '304128')
             then 168
         when FrequencyID in ('100022', '100124', '200517', '200591', '200944', '304140109') then 336
         when FrequencyID in ('100127', '200536') then 504
-        when FrequencyID in ('100128', '200537') then 672
+        when FrequencyID in ('27992', '100128', '200537') then 672
         when FrequencyID in ('200534') then 720
         when FrequencyID in ('100158', '304140108') then 728
         when FrequencyID in ('100129', '200982') then 840
@@ -120,7 +122,7 @@ select
         when FrequencyID in ('100161', '200558') then 2920
         when FrequencyID in ('100162') then 4280
         when FrequencyID in ('200559') then 4380
-        when FrequencyID in ('100163', '200946') then 8760
+        when FrequencyID in ('27993', '100163', '200946') then 8760
         when FrequencyID in ('200970') then 26280
         when FrequencyID in ('200973') then 35040
         when FrequencyID in ('200971', '99999999999') then 43800
-- 
GitLab