Kor03d

Untitled

May 22nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --we are only interested to find brand names that have 'stable' ingredient sets: with one possible ingredient combination
  2. drop table if exists brand_rx
  3. ;
  4. create table brand_rx as
  5. with bn_to_i as
  6.     (
  7.         select
  8.             c.concept_id as b_id,
  9.             r.concept_id_2 as i_id,
  10.             c.concept_name as concept_name,
  11.             count (r.concept_id_2) over (partition by c.concept_id) as cnt_direct
  12.         from concept c
  13.         join concept_relationship r on
  14.             r.relationship_id = 'Brand name of' and
  15.             c.concept_id = r.concept_id_1
  16.         join concept c2 on
  17.             c2.concept_class_id = 'Ingredient' and
  18.             c2.concept_id = r.concept_id_2
  19.         where
  20.             c.vocabulary_id in ('RxNorm', 'RxNorm Extension') and
  21.             c.concept_class_id = 'Brand Name' and
  22.             c.invalid_reason is null
  23.     ),
  24. bn_to_i_dp as --what possible ingredient sets drug products give us
  25.     (
  26.         select distinct
  27.             c.concept_id as b_id,
  28.             r.concept_id_2 as dp_id,
  29.             d.ingredient_concept_id as i_id,
  30.             count (d.ingredient_concept_id) over (partition by r.concept_id_2) as cnt_drug
  31.         from concept c
  32.         join concept_relationship r on
  33.             r.relationship_id = 'Brand name of' and
  34.             c.concept_id = r.concept_id_1
  35.         join concept c2 on
  36.             c2.concept_class_id != 'Ingredient' and --only combinations and ingredient themselves can have brand names;
  37.             c2.concept_id = r.concept_id_2
  38.         join drug_strength d on
  39.             c2.concept_id = d.drug_concept_id
  40.         where
  41.             c.vocabulary_id in ('RxNorm', 'RxNorm Extension') and
  42.             c.concept_class_id = 'Brand Name' and
  43.             c.invalid_reason is null
  44.     )
  45. select distinct
  46.     b.b_id,
  47.     b.concept_name,
  48.     b.i_id
  49. from bn_to_i b
  50. left join bn_to_i_dp d on
  51.     d.b_id = b.b_id and
  52.     b.cnt_direct > d.cnt_drug
  53. where d.b_id is null
  54. ;
  55. insert into brand_rx
  56. --preserve also bn that are consistent inside RxN
  57. with bn_to_i as
  58.     (
  59.         select
  60.             c.concept_id as b_id,
  61.             r.concept_id_2 as i_id,
  62.             c.concept_name as concept_name,
  63.             count (r.concept_id_2) over (partition by c.concept_id) as cnt_direct
  64.         from concept c
  65.         join concept_relationship r on
  66.             r.relationship_id = 'Brand name of' and
  67.             c.concept_id = r.concept_id_1
  68.         join concept c2 on
  69.             c2.concept_class_id = 'Ingredient' and
  70.             c2.concept_id = r.concept_id_2
  71.         where
  72.             c.concept_id not in (select b_id from brand_rx) and --avoid duplication
  73.             c.vocabulary_id = 'RxNorm' and
  74.             c.concept_class_id = 'Brand Name' and
  75.             c.invalid_reason is null and
  76.             exists
  77.             -- there are RxNorm Drug products with r.concept_id_2 as an ingredient
  78.                 (
  79.                     select
  80.                     from drug_strength d
  81.                     join concept x on
  82.                         d.drug_concept_id = x.concept_id and
  83.                         x.vocabulary_id = 'RxNorm' and
  84.                         x.concept_class_id != 'Ingredient' and
  85.                         d.ingredient_concept_id = r.concept_id_2
  86.                     -- with that brand name and ingredient
  87.                     join concept_relationship cr on
  88.                         cr.concept_id_1 = x.concept_id and
  89.                         relationship_id = 'Has brand name' and
  90.                         cr.concept_id_2 = c.concept_id
  91.                     where d.invalid_reason is null
  92.                 )
  93.     ),
  94. bn_to_i_dp as --what possible ingredient sets drug RxN products give us
  95.     (
  96.         select distinct
  97.             c.concept_id as b_id,
  98.             r.concept_id_2 as dp_id,
  99.             d.ingredient_concept_id as i_id,
  100.             count (d.ingredient_concept_id) over (partition by r.concept_id_2) as cnt_drug
  101.         from concept c
  102.         join concept_relationship r on
  103.             r.relationship_id = 'Brand name of' and
  104.             c.concept_id = r.concept_id_1
  105.         join concept c2 on
  106.             c2.concept_class_id != 'Ingredient' and --only combinations and ingredient themselves can have brand names;
  107.             c2.concept_id = r.concept_id_2
  108.         join drug_strength d on
  109.             c2.concept_id = d.drug_concept_id
  110.         where
  111.             c.concept_id not in (select b_id from brand_rx) and --avoid duplication
  112.             c.vocabulary_id = 'RxNorm' and
  113.             c.concept_class_id = 'Brand Name' and
  114.             c.invalid_reason is null and
  115.             d.invalid_reason is null
  116.     )
  117. select distinct
  118.     b.b_id,
  119.     b.concept_name,
  120.     b.i_id
  121. from bn_to_i b
  122. left join bn_to_i_dp d on
  123.     d.b_id = b.b_id and
  124.     b.cnt_direct > d.cnt_drug
  125. where d.b_id is null
  126. ;
Advertisement
Add Comment
Please, Sign In to add comment