how to do update data in 1 table from another table using with clause,select,storing language id's in json_array and join in postgresql -
if exists (select 1 lt_transmap_patient_language l left join lt_transmas_patient p on p.patient_id=l.patient_language_pat_id left join xch.xch_transmasmap_mpi_link m on m.mpi_lk_external_mpi=p.patient_mpi , m.mpi_lk_partner_id=2 m.mpi_lk_xch_mpi = _patient_mpi)then
if (_pat_lang_ids not null) update lt_transmap_patient_language l set patient_language_is_active = false lt_transmas_patient p join xch.xch_transmasmap_mpi_link m on m.mpi_lk_external_mpi=p.patient_mpi m.mpi_lk_xch_mpi=_patient_mpi; existinglanguages ( update lt_transmap_patient_language l set patient_language_is_active = pl.patient_language_is_active, patient_language_updated_by = pl.patient_language_created_by, patient_language_updated_on = pl.patient_language_updated_on, patient_language_language_id = pl.patient_language_language_id, patient_language_can_read = pl.patient_language_can_read, patient_language_can_write = pl.patient_language_can_write, patient_language_can_speak = pl.patient_language_can_speak, patient_language_proficiency = pl.patient_language_proficiency xch.xch_transmap_patient_language pl join xch.xch_transmas_patient p on p.patient_id=pl.patient_language_pat_id join xch.xch_transmasmap_mpi_link m on m.mpi_lk_xch_mpi=p.patient_mpi ( select (json_array_elements(_pat_lang_ids)->>'languageid')::bigint lang_id ) lang_l pl.patient_language_pat_id = _patient_id , patient_language_language_id=lang_list.lang_id returning patient_language_language_id ) end if; else insert lt_transmap_patient_language(patient_language_language_id,patient_language_pat_id, patient_language_created_by,patient_language_created_on,patient_language_can_read,patient_language_can_write, patient_language_can_speak,patient_language_proficiency) select patient_language_language_id, tp.patient_id,patient_language_created_by,patient_language_created_on, patient_language_can_read,patient_language_can_write,patient_language_can_speak,patient_language_proficiency xch.xch_transmap_patient_language l left join xch.xch_transmas_patient p on p.patient_id=l.patient_language_pat_id left join xch.xch_transmasmap_mpi_link m on m.mpi_lk_xch_mpi=p.patient_mpi , m.mpi_lk_partner_id=2 left join lt_transmas_patient tp on tp.patient_mpi=m.mpi_lk_external_mpi m.mpi_lk_xch_mpi=_patient_mpi , tp.patient_id not null; end if;
Comments
Post a Comment