@@ -256,7 +256,9 @@ WHERE r.RELA = 'has_inactive_ingredient'
256256 AND c2 .RXAUI = r .RXAUI2
257257 AND c2 .RXCUI = bd .rxcui ;
258258
259- -- Add NDCs for branded drugs not already in that table.
259+ -- Add NDCs for branded drugs not already in ndc_branded_drug or ndc_clinical_drug.
260+ -- Note that by starting with branded drugs, we prefer branded drug association
261+ -- in hte event that an NDC is associated with both a branded & a clinical drug.
260262INSERT OR IGNORE INTO ndc_branded_drug
261263SELECT DISTINCT
262264 NULL AS curie,
@@ -269,9 +271,10 @@ LEFT JOIN ndc_branded_drug AS n
269271WHERE s .RXCUI = bd .rxcui
270272 AND s .SAB = ' RXNORM'
271273 AND s .ATN = ' NDC'
272- AND n .curie IS NULL ;
274+ AND n .curie IS NULL
275+ AND s .ATV not in (select ndc from ndc_clinical_drug);
273276
274- -- Add NDCs for clinical drugs not already in that table .
277+ -- Add NDCs for clinical drugs not already in ndc_clinical_drug or ndc_branded_drug .
275278INSERT OR IGNORE INTO ndc_clinical_drug
276279SELECT DISTINCT
277280 NULL AS curie,
@@ -285,4 +288,29 @@ WHERE s.RXCUI = cd.rxcui
285288 AND s .SAB = ' RXNORM'
286289 AND s .ATN = ' NDC'
287290 AND n .curie IS NULL
288- AND s .ATV not in (select ndc from ndc_branded_drug);
291+ AND s .ATV not in (select ndc from ndc_branded_drug);
292+
293+ -- In the event that there is an NDC in ndc_clinical_drug, and that is now--
294+ -- in the current version of RxNorm that we are processing--
295+ -- associated with a branded drug, then we will isnert a row in
296+ -- ndc_branded_drug for this new association. Later, we will deal
297+ -- with the redundancy.
298+ INSERT INTO ndc_branded_drug
299+ SELECT DISTINCT
300+ n .curie as curie,
301+ n .ndc as ndc,
302+ bd .curie as drug
303+ FROM rxnorm .RXNSAT as s,
304+ branded_drug as bd,
305+ ndc_clinical_drug AS n
306+ WHERE s .ATV = n .ndc
307+ AND s .RXCUI = bd .RXCUI
308+ AND s .SAB = ' RXNORM'
309+ AND s .ATN = ' NDC' ;
310+
311+ -- Here, we delete anything that got duplicated in the previous step.
312+ -- If we updated the NDC to a new branded_drug association, then
313+ -- we created a new row in ndc_branded_drug, and to avoid duplicawtion
314+ -- now here will remove all such dups from ndc_clinical_drug
315+ DELETE from ndc_clinical_drug
316+ WHERE ndc in (select ndc from ndc_branded_drug);
0 commit comments