如何使用以下代码获取 1 行中的数据
Posted
技术标签:
【中文标题】如何使用以下代码获取 1 行中的数据【英文标题】:How to get data in 1 row using below code 【发布时间】:2017-04-24 11:15:15 【问题描述】:我想通过保持相应的 patient_id 来获取 1 行中的数据 iam 获取 1 行中的数据,但如果我通过提供函数 _patient_id 的输入参数执行 iam 数据将插入到 ams.ams_trans_integration_events 表中的 4 行中
elsif(_integration_type=34) 然后/患者注册/
select row_to_json(a.*) from (
select
m.mpi_ref_id as "refId",
p.patient_isactive as "patIsActive",
p.patient_created_by as "patientCreatedBy",
m.mpi_lk_external_mpi as "patientMpi",
p.patient_first_name as "patientFirstName",
p.patient_middle_name as "patientMiddleName",
p.patient_last_name as "patientLastName",
p.patient_gender as "patientGender",
p.patient_email as "patientEmail",
p.patient_birthdate as "patientDob",
p.patient_mobile::varchar as "patientMobile",
2 as "partnerId",
p.patient_maritalstatus AS "patientMaritalStatus",
p.patient_education_level AS "patientEducationLevel",
p.patient_identification_mark AS "patientIdentificationMark",
p.patient_is_estimated AS "patientIsEstimated",
p.patient_salutation AS "patientSalutation",
p.patient_blood_group AS "patientBloodGroup",
p.patient_mother_tongue AS "patientMotherTongue",
true AS "patIdentifierIsactive",
i.pat_identifier_dl AS "patIdentifierDl",
i.pat_identifier_uid AS "patIdentifierUid",
i.pat_identifier_pan_no AS "patIdentifierPanNo",
i.pat_identifier_passport AS "patIdentifierPassport",
(select json_agg(patDetails)
from (
select patient_language_language_id AS "languageId"
from xch.xch_transmap_patient_language
where patient_language_pat_id = _patient_id and patient_language_is_active = true
)patDetails) as "patientLanguageIds",
(select json_agg(patPersnlDetails)
from (
select cp.pat_cnt_land_line_no AS "homePhone",
cp.pat_cnt_mobile_no AS "mobile1",
cp.pat_cnt_email_id as "email",
cp.pat_cnt_land_line_no1 as "workPhone",
cp.pat_cnt_mobile_no1 as "mobile2",
cp.pat_cnt_mthd_of_cmctn as "mthdOfComm"
from xch.xch_transmasmap_patient_contact pc
left join xch.xch_transmas_patient_contact cp on cp.pat_cnt_id = pc.pat_contact_cnt_id
where pc.pat_contact_pat_id = _patient_id and pc.pat_contact_cnt_type_id = 1 and pc.pat_contact_isactive = true
)patPersnlDetails)as "patientPersnlDetails",
(select json_agg(patMailingAddressDetails)
from (
select cp.pat_cnt_address AS "address",
cp.pat_cnt_area AS "area",
cp.pat_cnt_pincode as "pinCode",
cp.pat_cnt_city as "city",
cp.pat_cnt_state as "state"
from xch.xch_transmasmap_patient_contact pc
left join xch.xch_transmas_patient_contact cp on cp.pat_cnt_id = pc.pat_contact_cnt_id
where pc.pat_contact_pat_id = _patient_id and pc.pat_contact_cnt_type_id = 4 and pc.pat_contact_isactive = true
)patMailingAddressDetails)as "patientMailingAddressDetails",
(select json_agg(patEmergencyCnt1Details)
from (
select cp.pat_cnt_first_name AS "firstName",
cp.pat_cnt_last_name AS "lastName",
cp.pat_cnt_relationship_id as "relationship",
cp.pat_cnt_email_id as "email",
cp.pat_cnt_land_line_no as "homePhone",
cp.pat_cnt_mobile_no AS "mobilePhone",
cp.pat_cnt_area AS "area",
cp.pat_cnt_pincode as "pinCode",
cp.pat_cnt_city AS "city",
cp.pat_cnt_state AS "state",
cp.pat_cnt_address as "address"
from xch.xch_transmasmap_patient_contact pc
left join xch.xch_transmas_patient_contact cp on cp.pat_cnt_id = pc.pat_contact_cnt_id
where pc.pat_contact_pat_id = _patient_id and pc.pat_contact_cnt_type_id = 2 and pc.pat_contact_isactive = true
)patEmergencyCnt1Details)as "patientEmergencyCnt1Details",
(select json_agg(patEmergencyCnt2Details)
from (
select cp.pat_cnt_first_name AS "firstName",
cp.pat_cnt_last_name AS "lastName",
cp.pat_cnt_relationship_id as "relationship",
cp.pat_cnt_email_id as "email",
cp.pat_cnt_land_line_no as "homePhone",
cp.pat_cnt_mobile_no AS "mobilePhone",
cp.pat_cnt_area AS "area",
cp.pat_cnt_pincode as "pinCode",
cp.pat_cnt_city AS "city",
cp.pat_cnt_state AS "state",
cp.pat_cnt_address as "address"
from xch.xch_transmasmap_patient_contact pc
left join xch.xch_transmas_patient_contact cp on cp.pat_cnt_id = pc.pat_contact_cnt_id
where pc.pat_contact_pat_id = _patient_id and pc.pat_contact_cnt_type_id = 3 and pc.pat_contact_isactive = true
)patEmergencyCnt2Details)as "patientEmergencyCnt2Details"/*,
(select json_agg(patTpaInsuranceDetails)
from (
select
itp.pat_policy_type_id as "patPolicyTypeId",
itp.pat_policy_proposer_relation AS "relationWithProposerEmployee",
itp.pat_policy_ins_company_id AS "insuranceCompany",
itp.pat_policy_ins_id AS "insurancePolicy",
itp.pat_policy_ins_grp_policy AS "insGroupPolicy",
itp.pat_policy_ins_valid_till AS "insPolicyValidTill",
itp.pat_policy_tpa_company_id AS "tpaCompany",
itp.pat_policy_tpa_id AS "tpaPolicy",
itp.pat_policy_tpa_grp_policy AS "tpaGroupPolicy",
itp.pat_policy_tpa_valid_till AS "tpaPolicyValidTill",
CASE WHEN _enc_order_ids = 0 THEN 0 ELSE itp.pat_policy_id END AS "patPolicyId",
itp.pat_policy_pat_enc_id as "policyPatEncId",
itp.pat_policy_created_by AS "patPolicyCreatedBy",
itp.pat_policy_isactive as "patPolicyIsactive"
from xch.xch_trans_patient_ins_tpa_policy itp
where itp.pat_policy_pat_id = _patient_id and itp.pat_policy_isactive = true
)patTpaInsuranceDetails)as "patientTpaInsuranceDetails"*/
from xch.xch_transmas_patient p
left join xch.xch_transmasmap_mpi_link m on m.mpi_lk_xch_mpi=p.patient_mpi
left join xch.xch_transmas_patient_identifier i on i.pat_identifier_pat_id=p.patient_id
--LEFT JOIN xch.xch_transmasmap_patient_contact c on c.pat_contact_pat_id=p.patient_id and c.pat_contact_isactive=true
--LEFT JOIN xch.xch_transmas_patient_contact pc on pc.pat_cnt_id=c.pat_contact_cnt_id and pc.pat_cnt_isactive=true
--LEFT JOIN xch.xch_trans_patient_ins_tpa_policy itp ON itp.pat_policy_pat_id=p.patient_id
where p.patient_id= _patient_id)a into integration_;
我将 4 行插入到 ams.ams_trans_integration_events 表中,如下所示,但我只需要插入 1 行,即最后一行
"refId":504,"patIsActive":true,"patientCreatedBy":218,"patientMpi":"2000000100000504","patientFirstName":"Aliya","patientMiddleName":null,"patientLastName":"T","patientGender":2,"patientEmail":"aliya@gmail.com","patientDob":"2000-01-02","patientMobile":"8496064235","partnerId":2,"patientMaritalStatus":null,"patientEducationLevel":null,"patientIdentificationMark":null,"patientIsEstimated":false,"patientSalutation":null,"patientBloodGroup":null,"patientMotherTongue":null,"patIdentifierIsactive":true,"patIdentifierDl":null,"patIdentifierUid":null,"patIdentifierPanNo":null,"patIdentifierPassport":null,"patientLanguageIds":null,"patientPersnlDetails":["homePhone":884422119900,"mobile1":8496064235,"email":"aliya@gmail.com","workPhone":9086747856,"mobile2":8843756874,"mthdOfComm":1],"patientMailingAddressDetails":["address":"6/2/201,Near Hosur Road","area":"ITPL","pinCode":684869,"city":46246,"state":1710],"patientEmergencyCnt1Details":["firstName":"Vamsi","lastName":"G","relationship":91,"email":"vamsi@gmail.com","homePhone":8899002244,"mobilePhone":9894689586,"area":"Shivananda Nagar","pinCode":898359,"city":46486,"state":1714,"address":"9/4/241,Near S.T.Joseph School"],"patientEmergencyCnt2Details":["firstName":"Smitha","lastName":"D","relationship":108,"email":"smitha@gmail.com","homePhone":8895685787,"mobilePhone":9057806789,"area":"Madhura Nagar","pinCode":574768,"city":103826,"state":4152,"address":"4/2/760,Near Minerva School"]
"refId":504,"patIsActive":true,"patientCreatedBy":218,"patientMpi":"2000000100000504","patientFirstName":"Aliya","patientMiddleName":null,"patientLastName":"T","patientGender":2,"patientEmail":"aliya@gmail.com","patientDob":"2000-01-02","patientMobile":"8496064235","partnerId":2,"patientMaritalStatus":null,"patientEducationLevel":null,"patientIdentificationMark":null,"patientIsEstimated":false,"patientSalutation":null,"patientBloodGroup":null,"patientMotherTongue":null,"patIdentifierIsactive":true,"patIdentifierDl":null,"patIdentifierUid":null,"patIdentifierPanNo":null,"patIdentifierPassport":null,"patientLanguageIds":null,"patientPersnlDetails":["homePhone":884422119900,"mobile1":8496064235,"email":"aliya@gmail.com","workPhone":9086747856,"mobile2":8843756874,"mthdOfComm":1],"patientMailingAddressDetails":["address":"6/2/201,Near Hosur Road","area":null,"pinCode":684869,"city":46246,"state":1710],"patientEmergencyCnt1Details":["firstName":"Vamsi","lastName":"G","relationship":91,"email":"vamsi@gmail.com","homePhone":8899002244,"mobilePhone":9894689586,"area":"Shivananda Nagar","pinCode":898359,"city":46486,"state":1714,"address":"9/4/241,Near S.T.Joseph School"],"patientEmergencyCnt2Details":["firstName":"Smitha","lastName":"D","relationship":108,"email":"smitha@gmail.com","homePhone":8895685787,"mobilePhone":9057806789,"area":"Madhura Nagar","pinCode":574768,"city":103826,"state":4152,"address":"4/2/760,Near Minerva School"]
"refId":504,"patIsActive":true,"patientCreatedBy":218,"patientMpi":"2000000100000504","patientFirstName":"Aliya","patientMiddleName":null,"patientLastName":"T","patientGender":2,"patientEmail":"aliya@gmail.com","patientDob":"2000-01-02","patientMobile":"8496064235","partnerId":2,"patientMaritalStatus":null,"patientEducationLevel":null,"patientIdentificationMark":null,"patientIsEstimated":false,"patientSalutation":null,"patientBloodGroup":null,"patientMotherTongue":null,"patIdentifierIsactive":true,"patIdentifierDl":null,"patIdentifierUid":null,"patIdentifierPanNo":null,"patIdentifierPassport":null,"patientLanguageIds":null,"patientPersnlDetails":["homePhone":884422119900,"mobile1":8496064235,"email":"aliya@gmail.com","workPhone":9086747856,"mobile2":8843756874,"mthdOfComm":1],"patientMailingAddressDetails":["address":"6/2/201,Near Hosur Road","area":null,"pinCode":684869,"city":46246,"state":1710],"patientEmergencyCnt1Details":["firstName":"Vamsi","lastName":"G","relationship":91,"email":"vamsi@gmail.com","homePhone":8899002244,"mobilePhone":9894689586,"area":"Shivananda Nagar","pinCode":898359,"city":46486,"state":1714,"address":"9/4/241,Near S.T.Joseph School"],"patientEmergencyCnt2Details":null
"refId":504,"patIsActive":true,"patientCreatedBy":218,"patientMpi":"2000000100000504","patientFirstName":"Aliya","patientMiddleName":null,"patientLastName":"T","patientGender":2,"patientEmail":"aliya@gmail.com","patientDob":"2000-01-02","patientMobile":"8496064235","partnerId":2,"patientMaritalStatus":null,"patientEducationLevel":null,"patientIdentificationMark":null,"patientIsEstimated":false,"patientSalutation":null,"patientBloodGroup":null,"patientMotherTongue":null,"patIdentifierIsactive":true,"patIdentifierDl":null,"patIdentifierUid":null,"patIdentifierPanNo":null,"patIdentifierPassport":null,"patientLanguageIds":null,"patientPersnlDetails":["homePhone":884422119900,"mobile1":8496064235,"email":"aliya@gmail.com","workPhone":9086747856,"mobile2":8843756874,"mthdOfComm":1],"patientMailingAddressDetails":["address":"6/2/201,Near Hosur Road","area":null,"pinCode":684869,"city":46246,"state":1710],"patientEmergencyCnt1Details":null,"patientEmergencyCnt2Details":null
【问题讨论】:
你想得到四行中的哪一行?.. 实际上我像这样进入 4 行 如果你得到 4 个相同的行,在SELECT
之后添加 DISTINCT
不,我的所有 4 行数据都不同,但我只想要最后一行数据
那么在哪里定义对应的子句?..
【参考方案1】:
改变
where p.patient_id= _patient_id)
到:
where p.patient_id= _patient_id and (select json_agg(patMailingAddressDetails)
from (
select cp.pat_cnt_address AS "address",
cp.pat_cnt_area AS "area",
cp.pat_cnt_pincode as "pinCode",
cp.pat_cnt_city as "city",
cp.pat_cnt_state as "state"
from xch.xch_transmasmap_patient_contact pc
left join xch.xch_transmas_patient_contact cp on cp.pat_cnt_id = pc.pat_contact_cnt_id
where pc.pat_contact_pat_id = _patient_id and pc.pat_contact_cnt_type_id = 4 and pc.pat_contact_isactive = true
)patMailingAddressDetails)->>'area' = 'ITPL')
【讨论】:
以上是关于如何使用以下代码获取 1 行中的数据的主要内容,如果未能解决你的问题,请参考以下文章
使用 python,我如何从 csv 中获取唯一行,但获取合并了哪些行(或行中的值)的记录?