/***************CREATE VIEWS*******************/
create or replace view 
	history.match_drugs_to_trials as
select nct_id,  rxcui, propvalue1
from 
	ctgov.browse_interventions as bi
	join
	rxnorm_migrated.rxnorm_props as rp
	on bi.downcase_mesh_term = rp.propvalue1 
where 
	propname='RxNorm Name'
	and 
	nct_id in (select nct_id from history.trial_snapshots)
;


/********************IN DEVLEOPMENT*********************/

/* Get the count of brand names attached to each trial
 * I should develop this into a view that matches trials to brands
 * then create a view that gets the counts.
 */
select rxcui1,count(rxcui2) from rxnorm_migrated.rxnorm_relations rr 
where 
	rxcui1 in (select rxcui from history.match_drugs_to_trials)
	and
	tty2 = 'BN'
group by rxcui1 
order by count(rxcui2) desc 
;