
drop view if exists public.match_trial_to_marketing_start_date;
DROP VIEW if exists public.match_trial_to_ndc11;
drop view if exists public.match_trials_to_bn_in;

drop view if exists history.match_drugs_to_trials;
DROP TABLE IF EXISTS history.trial_snapshots;

CREATE TABLE IF NOT EXISTS history.trial_snapshots
(
    nct_id character varying(15) COLLATE pg_catalog."default" NOT NULL,
    version integer NOT NULL,
    submission_date timestamp without time zone,
    primary_completion_date timestamp without time zone,
    primary_completion_date_category history.updatable_catetories,
    start_date timestamp without time zone,
    start_date_category history.updatable_catetories,
    completion_date timestamp without time zone,
    completion_date_category history.updatable_catetories,
    overall_status history.study_statuses,
    enrollment integer,
    enrollment_category history.updatable_catetories,
    sponsor character varying COLLATE pg_catalog."default",
    responsible_party character varying COLLATE pg_catalog."default",
    CONSTRAINT trial_snapshots_pkey PRIMARY KEY (nct_id, version)
);


ALTER TABLE IF EXISTS history.trial_snapshots
    OWNER to root;


CREATE OR REPLACE VIEW history.match_drugs_to_trials
AS SELECT bi.nct_id,
    rp.rxcui,
    rp.propvalue1
   FROM ctgov.browse_interventions bi
     JOIN rxnorm_migrated.rxnorm_props rp ON bi.downcase_mesh_term::text = rp.propvalue1::text
  WHERE rp.propname::text = 'RxNorm Name'::text AND (bi.nct_id::text IN ( SELECT trial_snapshots.nct_id
           FROM history.trial_snapshots));
           
       
CREATE OR REPLACE VIEW public.match_trials_to_bn_in
AS WITH trialncts AS (
         SELECT DISTINCT ts.nct_id
           FROM history.trial_snapshots ts
        )
 SELECT bi.nct_id,
    bi.downcase_mesh_term,
    rr.tty2,
    rr.rxcui2 AS bn_or_in_cui,
    count(*) AS count
   FROM ctgov.browse_interventions bi
     LEFT JOIN rxnorm_migrated.rxnorm_props rp ON bi.downcase_mesh_term::text = rp.propvalue1::text
     LEFT JOIN rxnorm_migrated.rxnorm_relations rr ON rr.rxcui1 = rp.rxcui
  WHERE (bi.nct_id::text IN ( SELECT trialncts.nct_id
           FROM trialncts)) AND bi.mesh_type::text = 'mesh-list'::text AND rp.propname::text = 'Active_ingredient_name'::text AND (rr.tty2 = ANY (ARRAY['BN'::bpchar, 'IN'::bpchar, 'MIN'::bpchar]))
  GROUP BY bi.nct_id, bi.downcase_mesh_term, rr.tty2, rr.rxcui2
  ORDER BY bi.nct_id;
  
  
CREATE OR REPLACE VIEW public.match_trial_to_ndc11
AS SELECT mttbi.nct_id,
    ah.ndc,
    count(*) AS count
   FROM match_trials_to_bn_in mttbi
     LEFT JOIN rxnorm_migrated.rxnorm_relations rr ON mttbi.bn_or_in_cui = rr.rxcui1
     LEFT JOIN rxnorm_migrated."ALLNDC_HISTORY" ah ON rr.rxcui2 = ah.rxcui
  WHERE rr.tty1 = 'BN'::bpchar AND (rr.tty2 = ANY (ARRAY['SBD'::bpchar, 'BPCK'::bpchar])) AND ah.sab::text = 'RXNORM'::text
  GROUP BY mttbi.nct_id, ah.ndc
  ORDER BY mttbi.nct_id, ah.ndc;
  
  
CREATE OR REPLACE VIEW public.match_trial_to_marketing_start_date
AS SELECT mttn.nct_id,
    n.application_number_or_citation,
    min(n.marketing_start_date) AS min
   FROM match_trial_to_ndc11 mttn
     JOIN spl.nsde n ON mttn.ndc = n.package_ndc11::bpchar
  WHERE n.product_type::text = 'HUMAN PRESCRIPTION DRUG'::text AND (n.marketing_category::text = ANY (ARRAY['NDA'::character varying, 'ANDA'::character varying, 'BLA'::character varying, 'NDA authorized generic'::character varying, 'NDA AUTHORIZED GENERIC'::character varying]::text[]))
  GROUP BY mttn.nct_id, n.application_number_or_citation
  ORDER BY mttn.nct_id;
  
