select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe';
select * from clin.encounter where fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe');
select * from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe');

select * from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe');

--select * from clin.clin_root_item where fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));

select * from clin.clin_narrative where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.allergy where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.lab_request where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.test_result  where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.vaccination where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.clin_medication where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.form_instances where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.clin_hx_family where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


select * from clin.clin_aux_note where  fk_episode in (select pk from clin.episode where fk_health_issue in ( select pk from clin.health_issue where  id_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe')) or fk_patient = (select pk_identity from dem.v_basic_person where lastnames ilike 'bloggs' and firstnames ilike 'joe'));


