Kor03d

Untitled

Jun 4th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. alter table ds_stage -- add mapped ingredient's concept_id to aid next step in dealing with dublicates
  2. add concept_id int4
  3. ;
  4. update ds_stage
  5. set concept_id =
  6.     (
  7.         select concept_id_2
  8.         from relationship_to_concept
  9.         where
  10.             concept_code_1 = ingredient_concept_code and
  11.             precedence = 1
  12.     )
  13. ;
  14. --Fix ingredients that got replaced/mapped as same one (e.g. Sodium ascorbate + Ascorbic acid => Ascorbic acid)
  15. drop table if exists ds_split
  16. ;
  17. create table ds_split as
  18. select distinct
  19.     drug_concept_code,
  20.     min (ingredient_concept_code :: bigint) over (partition by drug_concept_code, concept_id) :: varchar as ingredient_concept_code, --one at random
  21.     sum (amount_value) over (partition by drug_concept_code, concept_id) as amount_value,
  22.     amount_unit,
  23.     sum (numerator_value) over (partition by drug_concept_code, concept_id) as numerator_value,
  24.     numerator_unit,
  25.     denominator_value,
  26.     denominator_unit,
  27.     null :: int4 as box_size,
  28.     concept_id
  29. from ds_stage
  30. where
  31.     (drug_concept_code, concept_id) in
  32.     (
  33.         SELECT drug_concept_code, concept_id
  34.         FROM ds_stage
  35.         GROUP BY drug_concept_code, concept_id
  36.         HAVING COUNT(*) > 1
  37.     )
  38. ;
  39. delete from ds_stage
  40. where
  41.     (drug_concept_code, concept_id) in
  42.     (
  43.         SELECT drug_concept_code, concept_id
  44.         FROM ds_split
  45.     )
  46. ;
  47. insert into ds_stage
  48. select *
  49. from ds_split
  50. ;
  51. alter table ds_stage
  52. drop column concept_id
  53. ;
Advertisement
Add Comment
Please, Sign In to add comment